I have list a = ["string2" , "string4"]
and list b = ["string1" , "string2" , "string3" , "string4" , "string5"]
and I want to check if "string2" and "string4" from list a match those in list b and if it does, append list c with it's corresponding index in list b so list c should be [1,3]
My code so far:
for x in a:
for y in b:
if x == y:
print (x)
So I managed to print them out but don't know how to get the index.
Now this is the simpler version of my problem and I could just solve it like this but just for fun I will tell you the whole thing.
I have a list of tuples generated with nltk.word_tokenize in the following format [('string1', 'DT'), ('string2', 'NNP'), ('string3', 'NNP'), ('string4', 'NNP'), ('string5', 'VBZ'), ("string6", 'RB')]
and I want to check witch of the words(string1, string2, string3 etc) are found in another list of words (the stopwords list ex: stopwords = ["string312" , "string552" , string631"]
) and if found I would like to know their index in my list of tuples by creating another list that will store those indexes or remain empty if none found.
You can use
index
from your second list, while iterating over elements of the first list in a list comprehension.If there is a possibility that an element may be in
a
but not inb
then you can modify this slightly