【发布时间】:2013-07-29 00:19:18
【问题描述】:
我试图找到可用的方法,但找不到。没有contains。我应该使用index 吗?我只想知道该项目是否存在,不需要它的索引。
【问题讨论】:
标签: python collections tuples
我试图找到可用的方法,但找不到。没有contains。我应该使用index 吗?我只想知道该项目是否存在,不需要它的索引。
【问题讨论】:
标签: python collections tuples
你使用in。
if element in thetuple:
#whatever you want to do.
【讨论】:
sets,在那里你可以进行联合、差异和交叉操作。
if "word" in str(tuple):
# You can convert the tuple to str too
我有同样的问题,只有在转换 str() 后才对我有用
【讨论】:
__contains__ 重载运算符相同。因此,当原始发布者说没有__contains__ 时,他/她的意思是无法在数据集上使用 in 关键字。如您所见,字符串类已实现该运算符,因此您的答案没问题。继续! :D
请注意: 返回 哎呀。 使用集合:d= {...}
def simha():
d = ('this_is_valid')
b = 'valid'
if b in d:
print("Oops!!!!!")
simha()
【讨论】:
(element,)的符号时才会在python中创建。逗号非常重要。如果没有逗号,python 会将其视为要计算的表达式。