Home > Software design >  Python "in" operator not finding substring
Python "in" operator not finding substring

Time:06-06

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.

CodePudding user response:

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'
  • Related