mercredi 7 novembre 2018

Python: Match string in different conditions

I have 4 lists:

string1=['I love apple', 'Banana is yellow', "I have no school today", "Baking pies at home", "I bought 3 melons today"]
no=['strawberry','apple','melon', 'Banana', "cherry"]
school=['school', 'class']
home=['dinner', 'Baking', 'home']

I want to know every sting in string1 belongs to which group, if the string is about fruit, then ignore it, if string is about school and home, print them.

The result I expected:

I have no school today
school
Baking pies at home
Baking #find the first match

Here's my code, it did print out something I want, but with many duplicate values:

for i in string1:
    for j in no:
        if j in i:
            #print(j)
            #print(i)
            continue
        for k in school:
            if k in i:
                print(i)
                print(k)
            for l in home:
                if l in i:
                    print(i)
                    print(l)

I know this is not an efficient way to find the match. If you have any suggestion please let me know. Thank you!

Aucun commentaire:

Enregistrer un commentaire