我见过一个 index
函数,但它说如果找不到它就会出错。有没有一种简单的方法来检查该项目是否存在?我只想获得结果的 bool 值,例如:
if tuple.exists("item"):
print "it exists"
最佳答案
使用 in
运算符:
>>> 2 in (2, 3, 4)
True
in
运算符可用于检查序列中任何元素的成员资格。
并且不要将您的元组命名为 tuple
。使用不同的名称。
关于python - 有没有办法检查 Python 元组中是否存在某个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14902997/