为什么[范围(10)]和列表(范围(10))不一样?

时间:2021-12-26 10:25:14

Why are [range(10)] and list(range(10)) different in Python 3?

为什么Python 3中的[range(10)]和list(range(10))不同?

Output is as follows:

输出如下:

>>> print([range(10)])
[range(0, 10)]
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

4 个解决方案

#1


11  

Quoting the docs, "lists may be constructed in several ways":

引用文档,“列表可以通过多种方式构建”:

[range(10)]

builds a list of 1 item, the range object. In general a comma separated list of items enclosed in square brackets constructs a list of said items.

构建1项的列表,范围对象。通常,用方括号括起来的逗号分隔的项目列表构成所述项目的列表。

list(range(10))

passes the range object as an argument to the list constructor:

将范围对象作为参数传递给列表构造函数:

class list([iterable])

班级名单([可迭代])

The constructor builds a list whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].

构造函数构建一个列表,其项目与iterable的项目相同且顺序相同。 iterable可以是序列,支持迭代的容器,也可以是迭代器对象。如果iterable已经是一个列表,则会生成并返回一个副本,类似于iterable [:]。例如,list('abc')返回['a','b','c']和list((1,2,3))返回[1,2,3]。如果没有给出参数,构造函数将创建一个新的空列表[]。

A range in Python 3 represents an immutable sequence of numbers, so in your case the resulting list is a list of numbers from the range.

Python 3中的范围表示不可变的数字序列,因此在您的情况下,结果列表是范围中的数字列表。

#2


5  

[x] means "make a list whose one element is x".

[x]表示“制作一个元素为x的列表”。

list(x) means "make a list whose elements are the elements of x".

list(x)表示“创建一个列表,其元素是x的元素”。

range(10) returns an object that prints as range(0, 10) (since it shows the starting value when it prints) and whose elements are the integers from 0 to 9, so [range(10)] gives the one-element list [range(0, 10)] and list(range(10)) gives the 10-element list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

range(10)返回一个打印为范围(0,10)的对象(因为它显示打印时的起始值),其元素是从0到9的整数,因此[range(10)]给出了一个元素list [range(0,10)]和list(range(10))给出了10个元素的列表[0,1,2,3,4,5,6,7,8,9]。

#3


1  

In your first example you are simply creating a list object using square bracket notation, and placing a range() object inside. On the other hand, in your second example, you use the list() builtin to convert the range() object to a list.

在第一个示例中,您只是使用方括号表示法创建列表对象,并在其中放置一个range()对象。另一方面,在第二个示例中,使用list()内置将range()对象转换为列表。

The difference, is that using [] simply creates a list. It does nothing to the contents passed in. list() on the other hand, will either return an empty list if you pass nothing in, or attempt to convert what you passed in to a list object.

不同之处在于使用[]只是创建一个列表。它对传入的内容没有任何作用。另一方面,list()如果没有传入任何内容,将返回一个空列表,或者尝试将传入的内容转换为列表对象。

The documentation for list() notes this:

list()的文档说明了这一点:

The constructor builds a list whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].

构造函数构建一个列表,其项目与iterable的项目相同且顺序相同。 iterable可以是序列,支持迭代的容器,也可以是迭代器对象。如果iterable已经是一个列表,则会生成并返回一个副本,类似于iterable [:]。例如,list('abc')返回['a','b','c']和list((1,2,3))返回[1,2,3]。如果没有给出参数,构造函数将创建一个新的空列表[]。

#4


-1  

range() function in python 3.x is similar to the xrange() function in python 2.x.In Python 2.x range() produced a list, and xrange() returned an iterator - a sequence object

python 3.x中的range()函数类似于python 2.x中的xrange()函数。在Python 2.x range()中生成了一个列表,而xrange()返回了一个迭代器 - 一个序列对象

In python 2.x

在python 2.x中

>>> range(1)
[0]
>>> type(range(1))
<type 'list'>

In python 3.x

在python 3.x中

>>> range(1)
range(0, 1)
>>> type(range(1))
<class 'range'>

And to get a list, you can pass the generator in list()

要获得列表,您可以在列表中传递生成器()

>>> print (list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For further reference , read here

如需进一步参考,请阅读此处

#1


11  

Quoting the docs, "lists may be constructed in several ways":

引用文档,“列表可以通过多种方式构建”:

[range(10)]

builds a list of 1 item, the range object. In general a comma separated list of items enclosed in square brackets constructs a list of said items.

构建1项的列表,范围对象。通常,用方括号括起来的逗号分隔的项目列表构成所述项目的列表。

list(range(10))

passes the range object as an argument to the list constructor:

将范围对象作为参数传递给列表构造函数:

class list([iterable])

班级名单([可迭代])

The constructor builds a list whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].

构造函数构建一个列表,其项目与iterable的项目相同且顺序相同。 iterable可以是序列,支持迭代的容器,也可以是迭代器对象。如果iterable已经是一个列表,则会生成并返回一个副本,类似于iterable [:]。例如,list('abc')返回['a','b','c']和list((1,2,3))返回[1,2,3]。如果没有给出参数,构造函数将创建一个新的空列表[]。

A range in Python 3 represents an immutable sequence of numbers, so in your case the resulting list is a list of numbers from the range.

Python 3中的范围表示不可变的数字序列,因此在您的情况下,结果列表是范围中的数字列表。

#2


5  

[x] means "make a list whose one element is x".

[x]表示“制作一个元素为x的列表”。

list(x) means "make a list whose elements are the elements of x".

list(x)表示“创建一个列表,其元素是x的元素”。

range(10) returns an object that prints as range(0, 10) (since it shows the starting value when it prints) and whose elements are the integers from 0 to 9, so [range(10)] gives the one-element list [range(0, 10)] and list(range(10)) gives the 10-element list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

range(10)返回一个打印为范围(0,10)的对象(因为它显示打印时的起始值),其元素是从0到9的整数,因此[range(10)]给出了一个元素list [range(0,10)]和list(range(10))给出了10个元素的列表[0,1,2,3,4,5,6,7,8,9]。

#3


1  

In your first example you are simply creating a list object using square bracket notation, and placing a range() object inside. On the other hand, in your second example, you use the list() builtin to convert the range() object to a list.

在第一个示例中,您只是使用方括号表示法创建列表对象,并在其中放置一个range()对象。另一方面,在第二个示例中,使用list()内置将range()对象转换为列表。

The difference, is that using [] simply creates a list. It does nothing to the contents passed in. list() on the other hand, will either return an empty list if you pass nothing in, or attempt to convert what you passed in to a list object.

不同之处在于使用[]只是创建一个列表。它对传入的内容没有任何作用。另一方面,list()如果没有传入任何内容,将返回一个空列表,或者尝试将传入的内容转换为列表对象。

The documentation for list() notes this:

list()的文档说明了这一点:

The constructor builds a list whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].

构造函数构建一个列表,其项目与iterable的项目相同且顺序相同。 iterable可以是序列,支持迭代的容器,也可以是迭代器对象。如果iterable已经是一个列表,则会生成并返回一个副本,类似于iterable [:]。例如,list('abc')返回['a','b','c']和list((1,2,3))返回[1,2,3]。如果没有给出参数,构造函数将创建一个新的空列表[]。

#4


-1  

range() function in python 3.x is similar to the xrange() function in python 2.x.In Python 2.x range() produced a list, and xrange() returned an iterator - a sequence object

python 3.x中的range()函数类似于python 2.x中的xrange()函数。在Python 2.x range()中生成了一个列表,而xrange()返回了一个迭代器 - 一个序列对象

In python 2.x

在python 2.x中

>>> range(1)
[0]
>>> type(range(1))
<type 'list'>

In python 3.x

在python 3.x中

>>> range(1)
range(0, 1)
>>> type(range(1))
<class 'range'>

And to get a list, you can pass the generator in list()

要获得列表,您可以在列表中传递生成器()

>>> print (list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For further reference , read here

如需进一步参考,请阅读此处