The thing you are doing when you call if matches in checklist: is you are checking if the variable "matches" exists in "checklist".
This means you are looking if the list matches exists exactly like that in the list checklist.
But that's not what you actually want. You want to see if any of the entries in matches exists in any of the entries in checklist.
To check this you need to loop over these values. A loop means you basically do a lot of checks after another.
If you use a loop like:
for item in checklist:
You now have an loop that for every entry in checklist assumes the value of that item in a variable named "item".
Next you need to realize that your "matches" list also has multiple entries, so you need to loop over it as well:
for match in matches:
Will loop over every entry in matches and write the value in the variable "match".
Now you can compare the two and do a check:
if match in item:
This will trigger if the string in "match" exists in the string in "item".
So some code like
checklist=["A year on, AASU observes 'Black Day' to protest against CAA", u'Assam: Cargo reaches Lower Subansiri HE Project site despite protests', u'CAA will grant citizenship to 1.9 cr Hindu Bangladeshis in Assam: Akhil Gogoi', u'7 foods to increase estrogen levels in women', u'Meghalaya CM Conrad Sangma tests COVID-19 positive, has mild symptoms', u'AJYCP holds protest rally against CAA in Naharkatia', u"'Cash for job' scam: AMCH doctor's husband detained for interrogation", u"Meet 23-year-old Jojo Rajkumari, Manipur's first woman MMA fighter", u'Here are some food items to reduce heartburn and acid reflux']
matches = ["AASU", "Black Day"]
for item in checklist:
for match in matches:
if match in item:
print("found")
Will print "found" for every instance of a entry in "match" existing in your original list.
If you want to know the match simply modify it like this (I'll not rewrite the variable assignments from here on out)
for item in checklist:
for match in matches:
if match in item:
print("Found! \""+match+"\" was found in \""+item+"\"!")
If you want to know "where" in the list your entry was found you either need to use counting variables or work with indexes.
for i in range(len(checklist)):
for j in range(len(matches)):
if matches[j] in checklist[i]:
print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
As Mark Meyer noted this checks if the string in "matches" exists in the string in "checklist", so if you have a word that is part of a compound word it will return true.
For example if you have "sour" in the checklist it will consider "sourdough" a valid example. If you want to ONLY check for the word alone the way I'd do so is with some additional checks. This makes the program a lot more complicated and is not perfect, but you'll get the idea:
checklist=["A year on, AASU observes 'Black Day' to protest against CAA", u'Assam: Cargo reaches Lower Subansiri HE Project site despite protests', u'CAA will grant citizenship to 1.9 cr Hindu Bangladeshis in Assam: Akhil Gogoi', u'7 foods to increase estrogen levels in women', u'Meghalaya CM Conrad Sangma tests COVID-19 positive, has mild symptoms', u'AJYCP holds protest rally against CAA in Naharkatia', u"'Cash for job' scam: AMCH doctor's husband detained for interrogation", u"Meet 23-year-old Jojo Rajkumari, Manipur's first woman MMA fighter", u'Here are some food items to reduce heartburn and acid reflux']
matches = ["AASU", "Black Day"]
for i in range(len(checklist)):
for j in range(len(matches)):
if matches[j] in checklist[i]:
pos=checklist[i].find(matches[j])
if pos!=0:
if not checklist[i][pos-1].isalpha() and not checklist[i][pos+len(matches[j])].isalpha():
print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
elif pos==0:
if not checklist[i][pos+len(matches[j])].isalpha():
print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
The idea is to make sure it's not part of another word by checking the letter one before your match and one after and looking if it's a letter or something else. If it's a letter it's part of another word.
The check if "pos"==0 is done to make sure it's not at the beginning of the string, because then looking up the letter one before that would result in a out of range error.