python“ in”操作员找不到子字符串

发布于 2025-02-04 15:45:29 字数 946 浏览 2 评论 0原文

我试图发现子字符串列表中的任何子字符串是否在给定的字符串中。为此,我在列表的项目上循环,并使用Python的运算符中检查它们是否存在于字符串中。即使我确定其中一个子字符串存在于字符串中,我也会得到false值。我尝试在所有标题(子字符串)和我将它们匹配的文本上使用.lower()方法,并使用.lower()方法,我仍然尝试false> 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")

对于列表中的所有标题,我都会获得“否”。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-02-11 15:45:29

我尝试在两个标题上使用.lower()方法剥离所有空间...

而不是.lower(),您可以使用 casefolding 将连接“ file”正常化为“ fi”。

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

如果您想要类似的东西,但仍保持病例敏感性,请考虑 unidecode

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

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文