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

喜夏-厌秋 提交于 2019-11-30 01:02:29

问题


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.


回答1:


You use in.

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



回答2:


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()




回答3:


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

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


simha()


来源:https://stackoverflow.com/questions/17920147/how-to-check-if-a-tuple-contains-an-element-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!