[英]Comparing two list in a python
我必须比较两个列表。 List1 有句子,list2 有单词。 那么如何将 list2 单词与 list1 句子中的每个单词进行比较。
例如:
list1=['hi', 'computer', 'hello', 'world']
list2=['hi my name is computer','welcome to hello world','welcome to python']
所以在 output 中,我应该得到前 2 个句子,因为它们包含 list1 中的单词。
那么我该如何实现呢?
list1=['hi', 'computer', 'hello', 'world']
list2=['hi my name is computer','welcome to hello world','welcome to python']
output = []
for i in list2:
for j in list1:
if j in i:
output.append(i)
break
print(output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.