I want to compare word by word whether the word exists in the list.
items=["michael jackson","nintendo", "michael jackson"]
aa = ["i think michael jackson is cool","i love nintendo","i miss jackson nintendo"]
for i, a in zip(items, aa):
token=a.split()
for x in token:
if x in i:
print "X: " + x
Output:
X: i
X: michael
X: jackson
X: i
X: nintendo
X: i
X: jackson
Expected output:
X: michael X: jackson #from "i think michael jackson is cool"
X: nintendo #from i love nintendo"
X: jackson #from "i miss jackson nintendo"
As you can see, i
is also printed out because i
is in michael
and nintendo
, but I do not want that. Note that I want to compare words by words in items
and aa
, by comparing the items in the same index.
As for the 3rd item in items, it will print out jackson
although only jackson
is present when comparing with michael jackson
.
Note that the result for the 3rd item in aa should only be "jackson" but not "jackson", "nintendo" because the lists should be compared within the same index. the 3rd index in items is "michael jackson", there is no "nintendo"m hence the result should only be "jackson".
The first item matches "michael" and "jackson" so i would like the result to print them in one line so that the indexes in the results correspond to the index of items. Because if i proceeded with the original expected results, i realised that the order of the item in "aa" will be lost. Meaning to say, I would not know that "michael jackson" is extracted from the first item in "aa"