简体   繁体   中英

Comparing two list in a python

I have to compare two list. List1 is having a sentences and list2 is having words. SO how to compare the list2 words with list1 sentence each word.

For eg:

list1=['hi', 'computer', 'hello', 'world']
list2=['hi my name is computer','welcome to hello world','welcome to python']

so here in output I should get first 2 sentences as they have the words containing in list1.

SO how can I achieve it?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM