Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two list , the first list contains 3 lengthy sentences,

and my second list contains 3 words .

I need to compare second list with first list , whatever words present in the second list, if that word matches with the first list then I need to extract that word from second list .

Whatever matches found , I want to extract those words from the second list

but here it was comparing only one word ie ETA ,I am not getting idea .

can anyone help me

What I have tried:

Python
first_list=['AirLiquide_57304_Milestone Failed (subscription name): Shipment 8000421167 milestone Picked Up failed','Micron_59010,59007,708671_NEW ETA (subscription name): Shipment Number # "8000429230_0001" ETA', 'SAS_57217_719944_Milestone (subscription name): Shipment 8000421167 milestone Delivered failed.', 'AirLiquide_57304_Milestone Failed (subscription name): Shipment 8000421167 milestone Picked Up failed.']

second_list=['_milestone failed ', 'ETA', 'Delivery']
temp=0
for i in new_list:
    for j in range(len(val)):
        if i in val[j]:
            print(i)
            temp+=1
Posted
Updated 11-Nov-21 1:07am
v2
Comments
Richard MacCutchan 11-Nov-21 6:02am    
You do not make reference to either first_list or second_list in your code, so it will never work.

1 solution

The following code will print each entry from first_list that contains a word from second_list.
Python
first_list=['AirLiquide_57304_Milestone Failed (subscription name): Shipment 8000421167 milestone Picked Up failed','Micron_59010,59007,708671_NEW ETA (subscription name): Shipment Number # "8000429230_0001" ETA', 'SAS_57217_719944_Milestone (subscription name): Shipment 8000421167 milestone Delivered failed.', 'AirLiquide_57304_Milestone Failed (subscription name): Shipment 8000421167 milestone Picked Up failed.']

second_list=['_milestone failed ', 'ETA', 'Delivery']
temp=0
for sentence in first_list:
    for word in second_list:
        if word in sentence:
            print(sentence)


[edit]

As an alternative you could use a list comprehension like:
Python
final_list = [sentence for sentence in first_list for word in second_list if word in sentence ]
print(final_list)


[/edit]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900