简体   繁体   English

创建元组的单个元素元组

[英]Create a single element tuple of tuple

I just noticed that if you want to create a tuple with single element, being a tuple itself, you cannot do it with constructor tuple only with (,) syntax. 我只是注意到如果你想用单个元素创建一个元组,作为一个元组本身,你不能用构造函数tuple只用(,)语法来做。 Why is that? 这是为什么?

Example: 例:

>>> tuple(list('abc'))
('a', 'b', 'c')
>>> tuple(tuple(list('abc')))
('a', 'b', 'c')
>>> (tuple(list('abc')),)
(('a', 'b', 'c'),)

But then it holds for a list 但它有一个清单

>>> tuple([1],)
(1,)
>>> tuple([1])
(1,)

I don't really see the issue, this adheres to the documentation: 我没有真正看到这个问题,这符合文档:

 class tuple(object) | tuple() -> empty tuple | tuple(iterable) -> tuple initialized from iterable's items | | If the argument is a tuple, the return value is the same object. 

So, list('abc') always evaluates to ['a', 'b', 'c'] which is an iterable. 因此, list('abc')总是求值为['a', 'b', 'c'] ,这是一个可迭代的。

So in the first example ( tuple(['a', 'b', 'c']) ), the result is a tuple initialised from the iterable's items. 所以在第一个例子中( tuple(['a', 'b', 'c']) ),结果是从iterable的项初始化的元组。 Ie ('a', 'b', 'c'). 即('a','b','c')。

The second example takes the result of the first example (a tuple) and passes it into the tuple() function once more. 第二个示例获取第一个示例(元组)的结果,并再次将其传递给tuple()函数。 As the documentation states (last line), the return value when passed a tuple is the same object which matches with our result. 正如文档所述(最后一行),传递元组时的返回值是与我们的结果匹配的相同对象。

And for the third, once more, the docs tell us what we need to know: 对于第三个,再一次, 文档告诉我们需要知道的内容:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. 一个特殊的问题是构造包含0或1项的元组:语法有一些额外的怪癖来适应这些。 Empty tuples are constructed by an empty pair of parentheses; 空元组由一对空括号构成; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). 通过使用逗号跟随值来构造具有一个项目的元组(仅在括号中包含单个值是不够的)。


Finally, your last two examples ( tuple([1]) and tuple([1],) ) both produce a one-element tuple, because you are passing an iterable of length one. 最后,你的最后两个例子( tuple([1])tuple([1],) )都产生一个元素元组,因为你传递的是一个长度为1的迭代。 The docs once again state (at top): a tuple is initialized from iterable's items. 文档再次声明(顶部):元组是从iterable的项目初始化的。

So, to conclude, the case where you need a comma is when you want to create a tuple with one element. 因此,总而言之,您需要逗号的情况是您想要使用一个元素创建元组。 However, if passing an iterable of length one, this is not necessary as Python understands that you are not evaluating an expression. 但是,如果传递长度为1的可迭代,则不需要这样做,因为Python理解您没有评估表达式。


For completeness, the reason this awkward syntax is unavoidable is because statements like: (1 + 2) * 3 would evaluate to (3, 3, 3) rather than the expected 9 . 为了完整性,这种笨拙的语法不可避免的原因是因为: (1 + 2) * 3这样的语句将评估为(3, 3, 3)而不是预期的9 So instead, you must go out of your way through adding a comma: (1 + 2,) * 3 to get the result of (3, 3, 3) which makes perfect sense. 所以相反,你必须通过添加一个逗号: (1 + 2,) * 3来获得(3, 3, 3) 3,3,3)的结果,这是非常有意义的。

Without a trailing comma, Python will treat the value which you are passing as a expression. 如果没有尾随逗号,Python会将您传递的值视为表达式。

>>> tuple(tuple(list('abc')))

will give you 会给你

tuple(('a','b','c'))

Which is then evaluated into a tuple 然后将其评估为元组

('a','b','c')

Here is some more info if you need 如果需要,可以在这里获得更多信息

As for the list, tuple() takes iterables to create a tuple 至于列表,tuple()使用iterables来创建元组

its just the way it is. 就是这样。

Calling a constructor on an existing object does nothing (see list(list('1')) for example. Calling tuple(list('1')) would have to create a tuple , hence the , . This is not required from your 2nd call ( tuple(tuple(list('abc'))) ) where the inner tuple already exists, and therefore, "casting" it to a tuple does nothing 在现有对象上调用构造函数不会做任何事情(例如,请参阅list(list('1')) 。调用tuple(list('1'))必须创建一个tuple ,因此, 。,这不是你需要的。第二个调用( tuple(tuple(list('abc'))) )内部tuple已经存在,因此,将它“转换”为元组不会做任何事情

... you cannot do it with constructor tuple only with (,) syntax ...使用(,)语法只能使用构造函数元组

Yes you can. 是的你可以。 Simply you must add a comma when building a tuple containing one single element, because if you do not, you only get a parenthesed non tuple element: 只需在构建包含一个元素的元组时添加逗号,因为如果不这样做,则只获得一个带括号的非元组元素:

t = ((1,),)

is a tuple containing one single tuple containing integer 1. 是一个包含一个包含整数1的元组的元组。

But tuple only converts an iterable to a tuple. 但是tuple只将一个iterable转换为一个元组。 As such, it changes a string in a tuple of chars and changes a list in tuple. 因此,它会更改字符元组中的字符串并更改元组中的列表。 And if you pass a tuple, you just get a copy of the original tuple. 如果你传递一个元组,你只需要获得一个原始元组的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM