python判断字符串是否包含list中的元素

Posted by Tesla9527 on November 27, 2018

如下:

1
2
3
4
5
6
7
8
9
10
11
def containsAny(seq, aset):
    return True if any(i in seq for i in aset) else False
a = '我爱卓依婷'
list_b = ['爱', 'Timi']
list_c = ['不爱']
list_d = ['婷']
list_e = ['依婷']
print(containsAny(a, list_b))
print(containsAny(a, list_c))
print(containsAny(a, list_d))
print(containsAny(a, list_e))

输出:

1
2
3
4
True
False
True
True