python“ in”操作员找不到子字符串
我试图发现子字符串列表中的任何子字符串是否在给定的字符串中。为此,我在列表的项目上循环,并使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
.lower()
,您可以使用 casefolding 将连接“ file”正常化为“ fi”。如果您想要类似的东西,但仍保持病例敏感性,请考虑 unidecode :
Instead of
.lower()
, you could use casefolding which normalize the ligatures "fi" into "fi".If you want something similar, which is still keeping case-sensitivity, consider unidecode: