如何在python中匹配以下情况1 ..我希望句子中的每个单词都与列表匹配。
l1=['there is a list of contents available in the fields']
>>> 'there' in l1
False
>>> 'there is a list of contents available in the fields' in l1
True
答案 0 :(得分:1)
简单方法
l1=['there is a list of contents available in the fields']
>>> 'there' in l1[0]
True
更好的方法是迭代到列表的所有元素。
l1=['there is a list of contents available in the fields']
print(bool([i for i in l1 if 'there' in i]))
答案 1 :(得分:0)
如果您只是想知道列表中的任何字符串是否包含单词,无论它是哪个字符串,您都可以这样做:
if any('there' in element for element in li):
pass
现在,如果您想过滤与字符串匹配的字符串,您可以简单地:
li = filter(lambda x: 'there' in x, li)
或者在Python 3中:
li = list(filter(lambda x: 'there' in x, li))