当我想访问dict
中的tuple
的单个元素时,我得到了一个看起来很奇怪的错误
下面是dict
对象:
>>> x = {"palermo":{"country":"ARG",
... "utc":-3,
... "apply_time_change":(False), #other dicts are (True , "Region of the world")
... "hemisphere":"S"
... }}
>>> x['palermo']['apply_time_change'][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not subscriptable
真让人吃惊,但当我检查物体时:
>>> x['palermo']
{'country': 'ARG', 'utc': -3, 'apply_time_change': False, 'hemisphere': 'S'}
tuple
消失了。它打开了包装。我想知道在python3中,在dict
中打开iterable的alone元素是否是一个新功能,但事实并非如此,因为在list
中,它将alone元素保留在其中。为什么?它的目标是什么?因为对我来说它只会产生一个bug
Ubuntu 18.04.3下的Python 3.7.4
为了创建一个1元素元组,您需要使用
(False,)
相关问题 更多 >
编程相关推荐