如何在 Python 中创建一个空元组的元组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37895568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:05:29  来源:igfitidea点击:

How to create a tuple of an empty tuple in Python?

pythontuples

提问by Ali Shakiba

How can I create a tuple consisting of just an empty tuple, i.e. (())? I have tried tuple(tuple()), tuple(tuple(tuple())), tuple([])and tuple(tuple([]))which all gave me ().

我怎样才能创建一个只包含一个空元组的元组,即(())?我已经试过tuple(tuple())tuple(tuple(tuple()))tuple([])tuple(tuple([]))所有给我()

The reason that I use such a thing is as follows: Assume you have nbags with mitems. To represent a list of items in a bag, I use a tupleof length nwhere each element of that tuple is a representative for a bag. A bag might be empty, which is labeled by (). Now, at some initial point, I have just one bag with empty items!

我使用这种东西的原因如下:假设你有n装有m物品的袋子。为了表示包中的项目列表,我使用了tuple长度n,其中该元组的每个元素代表一个包。一个袋子可能是空的,标记为()。现在,在某个初始点,我只有一个装有空物品的袋子!

回答by Rory Daulton

The empty tuple is ()(or the more-verbose and slower tuple()), and a tuple with just one item (such as the integer 1), called a singleton (see hereand here) is (1,). Therefore, the tuple containing only the empty tuple is

空元组是()(或者更详细和更慢的tuple()),只有一个项目(例如整数1)的元组,称为单例(见这里这里)是(1,)。因此,只包含空元组的元组是

((),)

Here are some results showing that works:

以下是一些显示有效的结果:

>>> a=((),)
>>> type(a)
<type 'tuple'>
>>> len(a)
1
>>> a[0]
()
>>> type(a[0])
<type 'tuple'>
>>> len(a[0])
0

回答by juanpa.arrivillaga

I'm not surprised this (())didn't work, since the outer parentheses get interpreted as that - parentheses. So (()) == (), just like (2) == 2. This should work, however:

我并不感到惊讶这(())不起作用,因为外括号被解释为 - 括号。所以(()) == (),就像(2) == 2. 但是,这应该有效:

((),)

回答by innov8

tuple() is the only genuine empty tuple, but (), and ((),) create a tuple of length 1 that contains a tuple of length 0 - but not a tuple of length zero itself.

tuple() 是唯一真正的空元组,但是 () 和 ((),) 创建了一个长度为 1 的元组,其中包含一个长度为 0 的元组 - 但不是一个长度为零的元组本身。

If you want an answer to "how do I create an empty(or zero length) tuple.... I found this post with the search "how to create an empty tuple", then realised this was not the same question, but could be mistaken for that question (as the search does), so I though I would provide the answer to :

如果你想回答“我如何创建一个(或零长度)元组......我发现这篇文章搜索“如何创建一个空元组”,然后意识到这不是同一个问题,但可以被误认为是那个问题(就像搜索一样),所以我虽然我会提供以下答案:

How do you simply create an empty tuple?

你如何简单地创建一个空元组?

the original question could mislead you, as the original answers are almost good enough as an empty tuple, but do fail one test.

原始问题可能会误导您,因为原始答案作为一个空元组几乎足够好,但确实无法通过一项测试。

(),will create an 'empty' tuple as suggested in previous answers with ((),) which will also work, as will ((( ((( (),))) ))) in fact you can use any number of outer brackets you choose, they just work as brackets. However, python, when printing a tuple, does add one set of outer brackets.

(),将创建一个“空”元组,如先前​​答案中所建议的 ((),) 也将起作用,实际上 (((((((),))) ))) 实际上您可以使用任意数量的外括号你选择,他们只是作为括号工作。但是,python 在打印元组时会添加一组外括号。

empty brackets is a non-standard representation of 'no value' and adding the trailing comma makes a tuple from 'no value'. But it is a tuple with a 'no value' entry, not an empty tuple.

空括号是“无值”的非标准表示,添加尾随逗号会生成“无值”的元组。但它是一个带有“无值”条目的元组,而不是一个空元组。

Note: This is not a zero length tuple, as the other examples have also shown. The outer tuple is a tuple with one value, just that value has itself, is the empty tuple. So this creates an empty tuple insideanother tuple, and the other tuple is notempty. For a true empty tuple by itself, use tuple()although the (), behaves some what similar, it is not quite correct.

注意:这不是零长度元组,其他示例也已显示。外层元组是一个只有一个值的元组,只有那个值有它自己,就是空元组。所以这会另一个元组中创建一个空元组,而另一个元组不为空。对于一个真正的空元组本身,tuple()虽然使用 (),但行为有些相似,但并不完全正确。

>>>  a = (),
>>> type(a)
<class 'tuple'>
>>> len(a)
1
>>> a
((),)
>>> len(a[0])  # the inside tuple is empty, just not the outside one
0

Similarly, for a tuple of length 1 but with a value (of zero in the case of b, and "" for the example with c)

类似地,对于长度为 1 但具有值的元组(在 b 的情况下为零,对于 c 的示例,则为“”)

>>> b = 0,
>>> type(b)
<class 'tuple'>
>>> len(b)
1
>>>b
(0,)
# now with an empty string
>>> c = "",
>>> type(c)
<class 'tuple'>
>>> len(c)
1
>>>c
('',)
>>> len (c[0])  # same len of c[0] as with 'empty' tuple
0

So the outer brackets are included for displaying a tuple, but not actually part of the tuple, nor needed for creating the tuple.

因此,包含外部括号用于显示元组,但实际上不是元组的一部分,也不需要创建元组。

However all these brackets methods are not a real empty at the outer level, which is something that also has use cases.

然而,所有这些括号方法在外层并不是真正的空洞,这也有用例。

>>> a = ((),)  # extra brackets just to show same as other answers
>>> len(a)
1
>>> if a:
   print("not empty")
not empty
>>> e = tuple()
>>> len(e)
0
>>> type(e)
<class 'tuple'>
>>> if e:
   print("not empty")

>>> # note...did not print...so e acts as false as an empty tuple should

So if you really need a genuine empty tuple, use tuple(), but if near enough is all you need, you can use (),or ((),)

所以如果你真的需要一个真正的空元组,使用tuple(),但如果你需要的足够接近,你可以使用(),((),)