判断python中元组中的元组元素
>>> tuple6(1, 2, 4, ('dao', 'rou', 'yun'), 78, ('che', 'xiao'), 59)
假设我的元组如上,那我应怎么判断 'rou' 是否在 tuple6当中了呢?
试过了 'rou' in tuple6,结果是false,这个算正常的
当 'rou' in tuple6或者tuple(1)又或者是tuple6(1)等,都是提示
Traceback (most recent call last):
File "<pyshell#186>", line 1, in <module>
**********
TypeError: 'tuple' object is not callable
请各位大佬指点一下~
没有()这种用法,想取值只有用中括号[] def contain(iter, val):
for i in iter:
if i == val or hasattr(i, "__iter__") and contain(i, val):
return True
return False
contain(tuple6, "rou") >>> tuple6 = (1, 2, 4, ('dao', 'rou', 'yun'), 78, ('che', 'xiao'), 59)
>>> tuple6
(1, 2, 4, ('dao', 'rou', 'yun'), 78, ('che', 'xiao'), 59)
>>> 'rou' in tuple6
False
>>> 'rou' in tuple6
True
>>> 本帖最后由 nahongyan1997 于 2021-6-23 10:40 编辑
没有规律的序列在实际应用中没有任何用处,
不过还是忍不住想到了方法
a = (1, 2, 4, ('dao', 'rou', 'yun'), 78, ('che', 'xiao'), 59)
b = 'dao'
for each in a:
if isinstance(each,tuple):
for each1 in each:
if each1==b:
print("a",a.index(each))
print("each",each.index(each1))
else:
if each == b:
print("a",a.index(each))
页:
[1]