python:检查子字符串是否在字符串元组中
问题描述:
哪一种检查字符串元组中是否存在多个子字符串的最优雅方法?python:检查子字符串是否在字符串元组中
tuple = ('first-second', 'second-third', 'third-first')
substr1 = 'first'
substr2 = 'second'
substr3 = 'third'
#if substr1 in tuple and substr2 in tuple and substr3 in tuple:
# should return True
答
您需要遍历元组的每个子,因此使用any
和all
:
all(any(substr in s for s in data) for substr in ['first', 'second', 'third'])
+0
不错,我喜欢这个!谢谢。 –
不要使用'tuple'这是一个Python关键字。 – AChampion
@AChampion它不是一个关键字。如果是这样,分配给它甚至不会工作。 –
'tuple'是一个内建的 - 感谢您的纠正。 – AChampion