Python "in"运算符找不到子字符串



我试图找到子字符串列表中的任何子字符串是否在给定字符串中。为此,我循环遍历列表中的项,并使用python的in操作符检查它们是否存在于字符串中。我得到False值,即使我确定其中一个子字符串存在于字符串中。我试过剥离所有的空格,并使用.lower()方法对标题(子字符串)和我匹配它们的文本,我仍然得到False值。

我代码:


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")

列表中的所有标题都是"no"。

我已经尝试剥离所有空白并在两个标题上使用。lower()方法…

代替.lower(),你可以使用case - fold来使结扎符规范化。到"fi".

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

如果您想要类似的东西,但仍然保持大小写敏感,请考虑unidcode:

>>> from unidecode import unidecode
>>> unidecode("Significance")
'Significance'

最新更新