如何在 Python 中创建一个空元组的元组? [英] How to create a tuple of an empty tuple in Python?

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

问题描述

如何创建一个只包含一个空元组的元组,即 (())?我试过 tuple(tuple())tuple(tuple(tuple()))tuple([])tuple(tuple([])) 都给了我 ().

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

解决方案

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

((),)

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

<预><代码>>>>a=((),)>>>类型(一)<输入元组">>>>连(一)1>>>[0]()>>>类型(a[0])<输入元组">>>>len(a[0])0

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

The reason that I use such a thing is as follows: Assume you have n bags with m items. To represent a list of items in a bag, I use a tuple of length n where 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!

解决方案

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 here and here) is (1,). Therefore, the tuple containing only the empty tuple is

((),)

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

这篇关于如何在 Python 中创建一个空元组的元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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