mardi 26 juillet 2016

Python ; IF word in line and word2 not in line

Apologies for being a noob here , as i am a newbie in this

i have been trying to write a code which could find a certain words in line and then match from 2nd list of words to see if 2nd word exist in the same line. only print line only if first word exist not both in a line; here is the snippet and example

keywords =("a","b","c","d")
keywords2 = ("e","f","g")
a = ("a  is a ", "a is not e","b is b", "b is not f")
for key2 in keywords2:              
    for key in keywords:                

        for line in a:

                if key2 not in line:
                    if key in line:

                        print(key+" Found in --> ")
                        print      (line)

the output required is

a is a
b is b

while we have

a Found in --> 
a  is a 
b Found in --> 
b is b
b Found in --> 
b is not f
a Found in --> 
a  is a 
a Found in --> 
a is not e
b Found in --> 
b is b
a Found in --> 
a  is a 
a Found in --> 
a is not e
b Found in --> 
b is b
b Found in --> 
b is not f

i have tried few ways to implement the loop but to no use

Aucun commentaire:

Enregistrer un commentaire