为什么我可以使用布尔值作为列表索引?
Why can I use a Boolean as list index?
谁能帮我理解下面的 Python 代码?
In[41]: list_a = [1, 2, 3, 4]
In[42]: list_a[True]
Out[42]: 2
In[43]: list_a[False]
Out[43]: 1
True
被解释为 1 所以 list_a[True]
等同于 list_a[1]
list_a[False]
=> list_a[0]
相同
谁能帮我理解下面的 Python 代码?
In[41]: list_a = [1, 2, 3, 4]
In[42]: list_a[True]
Out[42]: 2
In[43]: list_a[False]
Out[43]: 1
True
被解释为 1 所以 list_a[True]
等同于 list_a[1]
list_a[False]
=> list_a[0]