>>> x=[1,2]
>>> x[1]
2
>>> x=(1,2)
>>> x[1]
2
Are they both valid? Is one preferred for some reason?
他们都是有效的吗?出于某种原因,你更喜欢它吗?
6 个解决方案
#1
194
Square brackets are lists while parentheses are tuples.
方括号是列表,圆括号是元组。
A list is mutable, meaning you can change its contents:
列表是可变的,这意味着您可以更改它的内容:
>>> x = [1,2]
>>> x.append(3)
>>> x
[1, 2, 3]
while tuples are not:
虽然元组不:
>>> x = (1,2)
>>> x
(1, 2)
>>> x.append(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
The other main difference is that a tuple is hashable, meaning that you can use it as a key to a dictionary, among other things. For example:
另一个主要区别是,tuple是可洗的,这意味着您可以将它用作字典的键,以及其他的东西。例如:
>>> x = (1,2)
>>> y = [1,2]
>>> z = {}
>>> z[x] = 3
>>> z
{(1, 2): 3}
>>> z[y] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Note that, as many people have pointed out, you can add tuples together. For example:
注意,正如许多人指出的,您可以将元组添加到一起。例如:
>>> x = (1,2)
>>> x += (3,)
>>> x
(1, 2, 3)
However, this does not mean tuples are mutable. In the example above, a new tuple is constructed by adding together the two tuples as arguments. The original tuple is not modified. To demonstrate this, consider the following:
然而,这并不意味着元组是可变的。在上面的示例中,通过将两个元组相加作为参数来构造一个新的元组。原始元组没有被修改。要证明这一点,请考虑以下几点:
>>> x = (1,2)
>>> y = x
>>> x += (3,)
>>> x
(1, 2, 3)
>>> y
(1, 2)
Whereas, if you were to construct this same example with a list, y
would also be updated:
然而,如果你用列表构造同样的例子,y也会被更新:
>>> x = [1, 2]
>>> y = x
>>> x += [3]
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
#2
4
They are not lists, they are a list and a tuple. You can read about tuples in the Python tutorial. While you can mutate lists, this is not possible with tuples.
它们不是列表,它们是列表和元组。您可以在Python教程中阅读有关元组的内容。虽然您可以更改列表,但元组是不可能的。
In [1]: x = (1, 2)
In [2]: x[0] = 3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/user/<ipython console> in <module>()
TypeError: 'tuple' object does not support item assignment
#3
2
The first is a list, the second is a tuple. Lists are mutable, tuples are not.
第一个是列表,第二个是元组。列表是可变的,元组不是。
Take a look at the Data Structures section of the tutorial, and the Sequence Types section of the documentation.
请看本教程的数据结构部分和文档的序列类型部分。
#4
2
Comma-separated items enclosed by (
and )
are tuple
s, those enclosed by [
and ]
are list
s.
由(和)包围的逗号分隔的项目是元组,由[和]包围的项目是列表。
#5
0
Another way brackets and parentheses differ is that square brackets can describe a list comprehension, e.g. [x for x in y]
方括号和括号的另一个不同之处是方括号可以描述列表理解,例如[x代表y中的x]
Whereas the corresponding parenthetic syntax specifies a tuple generator: (x for x in y)
而相应的括号语法指定了一个元组生成器:(x代表y中的x)
You can get a tuple comprehension using: tuple(x for x in y)
您可以使用:tuple(x代表y中的x)获得元组理解
See: Why is there no tuple comprehension in Python?
看:为什么Python中没有元组理解?
#6
0
One interesting difference that's worth noticing is when only a single value is there.
值得注意的一个有趣的差异是当只有一个值时。
lst=[1]
print lst // prints [1]
print type(lst) // prints <type 'list'>
notATuple=(1)
print notATuple // prints 1
print type(notATuple) // prints <type 'int'>
^^ instead of tuple(expected)
A comma must be included in a tuple even if it contains only a single value. e.g. (1,)
instead of (1)
.
一个逗号必须包含在一个元组中,即使它只包含一个值。例:1,而不是1。
#1
194
Square brackets are lists while parentheses are tuples.
方括号是列表,圆括号是元组。
A list is mutable, meaning you can change its contents:
列表是可变的,这意味着您可以更改它的内容:
>>> x = [1,2]
>>> x.append(3)
>>> x
[1, 2, 3]
while tuples are not:
虽然元组不:
>>> x = (1,2)
>>> x
(1, 2)
>>> x.append(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
The other main difference is that a tuple is hashable, meaning that you can use it as a key to a dictionary, among other things. For example:
另一个主要区别是,tuple是可洗的,这意味着您可以将它用作字典的键,以及其他的东西。例如:
>>> x = (1,2)
>>> y = [1,2]
>>> z = {}
>>> z[x] = 3
>>> z
{(1, 2): 3}
>>> z[y] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Note that, as many people have pointed out, you can add tuples together. For example:
注意,正如许多人指出的,您可以将元组添加到一起。例如:
>>> x = (1,2)
>>> x += (3,)
>>> x
(1, 2, 3)
However, this does not mean tuples are mutable. In the example above, a new tuple is constructed by adding together the two tuples as arguments. The original tuple is not modified. To demonstrate this, consider the following:
然而,这并不意味着元组是可变的。在上面的示例中,通过将两个元组相加作为参数来构造一个新的元组。原始元组没有被修改。要证明这一点,请考虑以下几点:
>>> x = (1,2)
>>> y = x
>>> x += (3,)
>>> x
(1, 2, 3)
>>> y
(1, 2)
Whereas, if you were to construct this same example with a list, y
would also be updated:
然而,如果你用列表构造同样的例子,y也会被更新:
>>> x = [1, 2]
>>> y = x
>>> x += [3]
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
#2
4
They are not lists, they are a list and a tuple. You can read about tuples in the Python tutorial. While you can mutate lists, this is not possible with tuples.
它们不是列表,它们是列表和元组。您可以在Python教程中阅读有关元组的内容。虽然您可以更改列表,但元组是不可能的。
In [1]: x = (1, 2)
In [2]: x[0] = 3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/user/<ipython console> in <module>()
TypeError: 'tuple' object does not support item assignment
#3
2
The first is a list, the second is a tuple. Lists are mutable, tuples are not.
第一个是列表,第二个是元组。列表是可变的,元组不是。
Take a look at the Data Structures section of the tutorial, and the Sequence Types section of the documentation.
请看本教程的数据结构部分和文档的序列类型部分。
#4
2
Comma-separated items enclosed by (
and )
are tuple
s, those enclosed by [
and ]
are list
s.
由(和)包围的逗号分隔的项目是元组,由[和]包围的项目是列表。
#5
0
Another way brackets and parentheses differ is that square brackets can describe a list comprehension, e.g. [x for x in y]
方括号和括号的另一个不同之处是方括号可以描述列表理解,例如[x代表y中的x]
Whereas the corresponding parenthetic syntax specifies a tuple generator: (x for x in y)
而相应的括号语法指定了一个元组生成器:(x代表y中的x)
You can get a tuple comprehension using: tuple(x for x in y)
您可以使用:tuple(x代表y中的x)获得元组理解
See: Why is there no tuple comprehension in Python?
看:为什么Python中没有元组理解?
#6
0
One interesting difference that's worth noticing is when only a single value is there.
值得注意的一个有趣的差异是当只有一个值时。
lst=[1]
print lst // prints [1]
print type(lst) // prints <type 'list'>
notATuple=(1)
print notATuple // prints 1
print type(notATuple) // prints <type 'int'>
^^ instead of tuple(expected)
A comma must be included in a tuple even if it contains only a single value. e.g. (1,)
instead of (1)
.
一个逗号必须包含在一个元组中,即使它只包含一个值。例:1,而不是1。