I want to know that why adding a trailing comma after a variable name (in this case a string) makes it a tuple
. i.e.
我想知道为什么在变量名后添加一个尾随逗号(在本例中是一个字符串)使它成为一个元组。即
>>> abc = 'mystring',
>>> print(abc)
('mystring',)
When I print abc
it returns the tuple
('mystring',)
.
当我打印abc时,它返回元组('mystring',)。
6 个解决方案
#1
34
It is the commas, not the parentheses, which are significant. The Python tutorial says:
它是逗号,而不是括号,它们很重要。 Python教程说:
A tuple consists of a number of values separated by commas
元组由逗号分隔的许多值组成
Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.
括号用于在使用逗号的其他地方消除歧义,例如,使您能够嵌套或输入元组作为参数列表的一部分。
See the Python Tutorial section on Tuples and Sequences
请参阅元组和序列的Python教程部分
#2
7
Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.
因为这是用一个元素编写元组文字的唯一方法。对于列表文字,必要的括号使语法唯一,但由于parantheses也可以表示分组,将括号中的表达式括起来不会将其转换为元组:您需要一个不同的语法元素,在本例中为逗号。
#3
4
Update
See above for a much better answer.
请参阅上文以获得更好的答案。
Original Answer
In python a tuple is indicated by parenthesis
.
在python中,元组用括号表示。
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the
__str__
and__repr__
methods on a tuple will show them.括号中没有指示元组。任何表达式都可以括在括号中,这对于元组来说并不是特别的。事实上,几乎总是需要使用括号,因为否则它将是模糊的,这就是元组上的__str__和__repr__方法将显示它们的原因。
I stand corrected (all I've been doing today. Sigh).
我的立场得到了纠正(我今天一直都在做。叹气)。
For instance:
abc = ('my', 'string')
What about single element tuples? The parenthesis notation still holds.
单元素元组怎么样?括号表示法仍然有效。
abc = ('mystring',)
For all tuples, the parenthesis can be left out but the comma needs to be left in.
对于所有元组,可以省略括号,但需要保留逗号。
abc = 'mystring', # ('mystring',)
Or
abc = 'my', 'string', # ('my', 'string',)
So in effect what you were doing was to create a single element tuple as opposed to a string.
所以实际上你正在做的是创建单个元素元组而不是字符串。
The documentation clearly says:
文件清楚地说:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
包含至少一个逗号的表达式列表产生一个元组。元组的长度是列表中表达式的数量。表达式从左到右进行评估。
#4
1
Unpacking multi-element tuple:
a, b = (12, 14)
print type(a)
Output:
int
Unpacking single-element tuple:
a, = (12, )
print type(a)
Output:
int
Otherwise:
a = (12,)
print type(a)
Output:
tuple
#5
1
When you see a comma after a single value, that value is interpreted as the datatype 'tuple'.
当您在单个值后面看到逗号时,该值将被解释为数据类型'tuple'。
Here is a little something I've learned through experience that may apply to some of you:
这是我通过经验学到的一些东西,可能适用于你们中的一些人:
If you're a musician, the word tuple may be confusing, since the words tuple and triple are used to describe groupings of notes that are used within a certain type of time signature that they are not strictly compatible with. For example a grouping of two eighth notes played as if the time signature were 4/4 (straight feel) when the time signature is 6/8 (triplet feel). Or vice versa a triplet played in 4/4 time. This leads the novice programmer to perhaps interpret a tuple as a pair of values.
如果您是音乐家,单词元组可能会令人困惑,因为单词元组和三元组用于描述在特定类型的时间签名中使用的音符分组,这些音符不是严格兼容的。例如,当时间签名是6/8(三重感觉)时,两个八分音符的分组就像时间特征是4/4(直感)一样。反之亦然,三重奏在4/4时间播放。这导致新手程序员可能将元组解释为一对值。
This isn't the same kind of tuple as you see in programming. These tuples are an immutable (un-alterable once assigned) sequence data type that can hold any number of values but can be considered to be transferred together as if they were all enclosed between to parentheses, or in other words, a tuple of parentheses.
这与你在编程中看到的元组不同。这些元组是一个不可变的(不可改变的一次分配的)序列数据类型,它可以保存任意数量的值但可以被认为是一起转移,就像它们全部被括在括号之间,或者换句话说,括号的元组。
You can't add or delete stuff from a tuple once it is assigned, so it is usually used to pack and unpack variables. I use it frequently to return multiple values from a function:
分配后,您无法在元组中添加或删除元素,因此通常用于打包和解包变量。我经常使用它来从函数返回多个值:
def somefunction_foo(some_data_file):
map1 = dict()
map2 = dict()
map3 = dict()
with open(datafile, 'r') as file: # auto-close the file after this block
for row in file:
pass
# don't actually pass, but
# fill each map with specific data from the same file
return map1, map2, map3 # I'm returning a tuple, but without parenthesis
#6
0
In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.
在问题的示例中,您将变量'abc'分配给长度为1的元组。
You can do multiple assignments with this similar syntax:
您可以使用以下类似语法执行多项分配:
x,y = 20,50
Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.
另请注意,print语句对使用逗号结束print语句有特殊的理解;这告诉print省略尾随换行符。
print 'hello',
print 'world'
result:
hello world
#1
34
It is the commas, not the parentheses, which are significant. The Python tutorial says:
它是逗号,而不是括号,它们很重要。 Python教程说:
A tuple consists of a number of values separated by commas
元组由逗号分隔的许多值组成
Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.
括号用于在使用逗号的其他地方消除歧义,例如,使您能够嵌套或输入元组作为参数列表的一部分。
See the Python Tutorial section on Tuples and Sequences
请参阅元组和序列的Python教程部分
#2
7
Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.
因为这是用一个元素编写元组文字的唯一方法。对于列表文字,必要的括号使语法唯一,但由于parantheses也可以表示分组,将括号中的表达式括起来不会将其转换为元组:您需要一个不同的语法元素,在本例中为逗号。
#3
4
Update
See above for a much better answer.
请参阅上文以获得更好的答案。
Original Answer
In python a tuple is indicated by parenthesis
.
在python中,元组用括号表示。
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the
__str__
and__repr__
methods on a tuple will show them.括号中没有指示元组。任何表达式都可以括在括号中,这对于元组来说并不是特别的。事实上,几乎总是需要使用括号,因为否则它将是模糊的,这就是元组上的__str__和__repr__方法将显示它们的原因。
I stand corrected (all I've been doing today. Sigh).
我的立场得到了纠正(我今天一直都在做。叹气)。
For instance:
abc = ('my', 'string')
What about single element tuples? The parenthesis notation still holds.
单元素元组怎么样?括号表示法仍然有效。
abc = ('mystring',)
For all tuples, the parenthesis can be left out but the comma needs to be left in.
对于所有元组,可以省略括号,但需要保留逗号。
abc = 'mystring', # ('mystring',)
Or
abc = 'my', 'string', # ('my', 'string',)
So in effect what you were doing was to create a single element tuple as opposed to a string.
所以实际上你正在做的是创建单个元素元组而不是字符串。
The documentation clearly says:
文件清楚地说:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
包含至少一个逗号的表达式列表产生一个元组。元组的长度是列表中表达式的数量。表达式从左到右进行评估。
#4
1
Unpacking multi-element tuple:
a, b = (12, 14)
print type(a)
Output:
int
Unpacking single-element tuple:
a, = (12, )
print type(a)
Output:
int
Otherwise:
a = (12,)
print type(a)
Output:
tuple
#5
1
When you see a comma after a single value, that value is interpreted as the datatype 'tuple'.
当您在单个值后面看到逗号时,该值将被解释为数据类型'tuple'。
Here is a little something I've learned through experience that may apply to some of you:
这是我通过经验学到的一些东西,可能适用于你们中的一些人:
If you're a musician, the word tuple may be confusing, since the words tuple and triple are used to describe groupings of notes that are used within a certain type of time signature that they are not strictly compatible with. For example a grouping of two eighth notes played as if the time signature were 4/4 (straight feel) when the time signature is 6/8 (triplet feel). Or vice versa a triplet played in 4/4 time. This leads the novice programmer to perhaps interpret a tuple as a pair of values.
如果您是音乐家,单词元组可能会令人困惑,因为单词元组和三元组用于描述在特定类型的时间签名中使用的音符分组,这些音符不是严格兼容的。例如,当时间签名是6/8(三重感觉)时,两个八分音符的分组就像时间特征是4/4(直感)一样。反之亦然,三重奏在4/4时间播放。这导致新手程序员可能将元组解释为一对值。
This isn't the same kind of tuple as you see in programming. These tuples are an immutable (un-alterable once assigned) sequence data type that can hold any number of values but can be considered to be transferred together as if they were all enclosed between to parentheses, or in other words, a tuple of parentheses.
这与你在编程中看到的元组不同。这些元组是一个不可变的(不可改变的一次分配的)序列数据类型,它可以保存任意数量的值但可以被认为是一起转移,就像它们全部被括在括号之间,或者换句话说,括号的元组。
You can't add or delete stuff from a tuple once it is assigned, so it is usually used to pack and unpack variables. I use it frequently to return multiple values from a function:
分配后,您无法在元组中添加或删除元素,因此通常用于打包和解包变量。我经常使用它来从函数返回多个值:
def somefunction_foo(some_data_file):
map1 = dict()
map2 = dict()
map3 = dict()
with open(datafile, 'r') as file: # auto-close the file after this block
for row in file:
pass
# don't actually pass, but
# fill each map with specific data from the same file
return map1, map2, map3 # I'm returning a tuple, but without parenthesis
#6
0
In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.
在问题的示例中,您将变量'abc'分配给长度为1的元组。
You can do multiple assignments with this similar syntax:
您可以使用以下类似语法执行多项分配:
x,y = 20,50
Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.
另请注意,print语句对使用逗号结束print语句有特殊的理解;这告诉print省略尾随换行符。
print 'hello',
print 'world'
result:
hello world