Python元组比较奇怪的行为 [英] Python tuple comparison odd behavior

查看:32
本文介绍了Python元组比较奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下这种行为吗?

Can someone please explain this behavior?

In[11]: (1, 2) in [(True, 2)]
Out[11]: True

In[12]: (1, 2) in [(True, True)]
Out[12]: False

In[13]: (1, 2) in [(True, False)]
Out[13]: False

In[14]: ("1", 2) in [(True, 2)]
Out[14]: False

感觉就像一个错误——每当我检查一个元组是否在一个元组列表中时,整数 1 总是等于 True.如果可以解释,我不想将其报告给 Python 错误跟踪器.

It feels like a bug--whenever I check if a tuple is in a list of tuples, the integer 1 is always equal to True. I don't want to report it to the Python bug tracker if it can be explained.

推荐答案

这是True 的实现细节,参见 Python 中的 False == 0 和 True == 1 是实现细节还是它由语言保证?进行讨论.

It's something of an implementation detail of True, see Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language? for a discussion.

这篇关于Python元组比较奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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