创建元组的单元素元组 [英] Create a single element tuple of tuple

查看:60
本文介绍了创建元组的单元素元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到,如果你想创建一个具有单个元素的元组,作为元组本身,你不能用构造函数 tuple 只用 (,) 语法来做.这是为什么?

示例:

<预><代码>>>>元组(列表('abc'))('a', 'b', 'c')>>>元组(元组(列表('abc')))('a', 'b', 'c')>>>(元组(列表('abc')),)(('a', 'b', 'c'),)

但是它适用于一个列表

<预><代码>>>>元组([1],)(1,)>>>元组([1])(1,)

解决方案

我真的没有看到这个问题,这符合文档:

<块引用>

类元组(对象)|元组()->空元组|元组(可迭代)->从可迭代项初始化的元组||如果参数是元组,则返回值是同一个对象.

<小时>

所以,list('abc') 总是计算为 ['a', 'b', 'c'] 这是一个可迭代的.

因此在第一个示例 (tuple(['a', 'b', 'c'])) 中,结果是一个从可迭代项初始化的元组.IE.('a', 'b', 'c').

第二个示例采用第一个示例(一个元组)的结果,并将其再次传递给 tuple() 函数.正如文档所述(最后一行),传递元组时的返回值是与我们的结果匹配的同一对象.

对于第三个,文档 告诉我们需要知道的:

<块引用>

一个特殊的问题是包含 0 或 1 个项目的元组的构造:语法有一些额外的怪癖来适应这些.空元组由一对空括号构成;包含一项的元组是通过在值后面加上逗号来构造的(将单个值括在括号中是不够的).

<小时>

最后,您的最后两个示例(tuple([1])tuple([1],))都生成一个单元素元组,因为您是传递长度为 1 的可迭代对象.文档再次声明(在顶部):元组是从可迭代的项目中初始化的.

所以,总而言之,需要逗号的情况是当您想创建一个包含一个元素的元组时.但是,如果传递长度为 1 的可迭代对象,则没有必要这样做,因为 Python 知道您不是在评估表达式.

<小时>

为了完整起见,这种笨拙的语法不可避免的原因是,像这样的语句:(1 + 2) * 3 将评估为 (3, 3, 3) 而不是比预期的 9.因此,您必须特意添加一个逗号:(1 + 2,) * 3 以获得 (3, 3, 3) 的结果,其中很有道理.

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. 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.

So in the first example (tuple(['a', 'b', 'c'])), the result is a tuple initialised from the iterable's items. I.e. ('a', 'b', 'c').

The second example takes the result of the first example (a tuple) and passes it into the tuple() function once more. 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. 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. The docs once again state (at top): a tuple is initialized from iterable's items.

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.


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. 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.

这篇关于创建元组的单元素元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆