检查字符串元组中是否出现多个子字符串的最优雅方法是什么?
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
答案 0 :(得分:2)
any(substr in str_ for str_ in tuple_)
您可以从此开始,同时查看all()
。
答案 1 :(得分:2)
您需要遍历每个子字符串的元组,因此使用any
和all
:
all(any(substr in s for s in data) for substr in ['first', 'second', 'third'])