Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python "in" operator not finding substring

I am trying to find if any substring in a list of substrings is in a given string. To do so, I loop over the items of the list and check if they exist in the string using python’s in operator. I am getting False values even though I am sure one of the substring exists in the string. I have tried stripping all whitespace and using the .lower() method on both the titles (substrings) and the text I am matching them to and I still get False values throughout.

My code:


example = "Research Policy journal homepage: www.elsevier.com/locate/respol Editorial Introduction to special section on university–industry linkages: The significance of tacit knowledge and the role of intermediaries The papers in this special section of research World Bank study on the growth prospects of the leading East Asian economies."

list_of_titles = ["Introduction to special section on university–industry linkages: The significance of tacit knowledge and the role of intermediaries", "another title", "another title"]

for title in list_of_titles:
   if title in example:
       print("Yes")
   else:
       print("No")

I get "No"s for all titles in the list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’ve tried stripping all whitespace and using the .lower() method on both the titles …

Instead of .lower(), you could use casefolding which normalize the ligatures "fi" into "fi".

>>> "significance".casefold() == "significance"
True

If you want something similar, which is still keeping case-sensitivity, consider unidecode:

>>> from unidecode import unidecode
>>> unidecode("Significance")
'Significance'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading