如何检查元组是否包含 Python 中的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17920147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:29:06  来源:igfitidea点击:

How to check if a tuple contains an element in Python?

pythoncollectionstuples

提问by Joan Venge

I tried to find the available methods but couldn't find it. There is no contains. Should I use index? I just want to know if the item exists, don't need the index of it.

我试图找到可用的方法,但找不到。没有contains。我应该使用index吗?我只想知道该项目是否存在,不需要它的索引。

采纳答案by Lennart Regebro

You use in.

你用in.

if element in thetuple:
    #whatever you want to do.

回答by Eitan Bendersky

Be careful with that: return Oops. use Set: d= {...}

小心:返回哎呀。使用集合:d= {...}

def simha():
    d = ('this_is_valid')
    b = 'valid'
    if b in d:
        print("Oops!!!!!")


simha()

回答by magusvox

if "word" in str(tuple):
# You can convert the tuple to str too

i has the same problem and only worked to me after the convert str()

我有同样的问题,只有在转换 str() 后才对我有用