【问题标题】:why same index is being shown by python in this tuple?为什么 python 在这个元组中显示相同的索引?
【发布时间】:2021-01-12 02:32:53
【问题描述】:

在:my_tuple=('a',9,1,True,'a', [2,5])

在:my_tuple.index(1)、my_tuple.index(True)

输出:(2, 2)

谁能解释到底发生了什么?为什么条目 True 和 1 的索引显示相同?

【问题讨论】:

标签: python-3.x data-structures tuples


【解决方案1】:

自True == 1:

>>> 1 == True
True
>>> (1, True,).index(True)
0

bool 是 Python 中 int 的子类,在 Python 3.x 中,您将始终拥有 False = 0 和 True = 1。

>>> isinstance(True, int)
True
>>> int(True)
1

【讨论】:

    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多