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

Joa*_*nge 16 python collections tuples

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

Len*_*bro 48

你用in.

if element in thetuple:
    #whatever you want to do.
Run Code Online (Sandbox Code Playgroud)


小智 10

if "word" in str(tuple):
# You can convert the tuple to str too
Run Code Online (Sandbox Code Playgroud)

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