python:检查substring是否在字符串元组中

时间:2016-10-01 14:02:55

标签: python substring tuples

检查字符串元组中是否出现多个子字符串的最优雅方法是什么?

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

2 个答案:

答案 0 :(得分:2)

any(substr in str_ for str_ in tuple_)

您可以从此开始,同时查看all()

答案 1 :(得分:2)

您需要遍历每个子字符串的元组,因此使用anyall

all(any(substr in s for s in data) for substr in ['first', 'second', 'third'])