我会尽量详细解释我需要什么:
我正在使用 feedparser 在 Python 中解析 RSS 提要。当然,这个提要有一个项目列表,带有标题、链接和描述,就像一个普通的 RSS 提要一样。
另一方面,我有一个字符串列表,其中包含一些我需要在项目描述中找到的关键字。
我需要做的是找到关键字匹配最多的项目
例子:
RSS 提要
<channel>
<item>
<title>Lion</title>
<link>...</link>
<description>
The lion (Panthera leo) is one of the four big cats in the genus
Panthera, and a member of the family Felidae.
</description>
</item>
<item>
<title>Panthera</title>
<link>...</link>
<description>
Panthera is a genus of the Felidae (cats), which contains
four well-known living species: the tiger, the lion, the jaguar, and the leopard.
</description>
</item>
<item>
<title>Cat</title>
<link>...</link>
<description>
The domestic cat is a small, usually furry, domesticated,
carnivorous mammal. It is often called the housecat, or simply the
cat when there is no need to distinguish it from other felids and felines.
</description>
</item>
</channel>
关键字列表
['cat', 'lion', 'panthera', 'family']
所以在这种情况下,具有最多(唯一)匹配项的项目是第一个,因为它包含所有 4 个关键字(不管它说的是“猫”而不是“猫”,我只需要找到字符串中的文字关键字)
让我澄清一下,即使某些描述包含“猫”关键字 100 次(没有其他关键字),这也不会是赢家,因为我正在寻找包含最多的关键字,而不是最多次数出现关键字。
现在,我正在遍历 rss 项目并“手动”执行此操作,计算关键字出现的次数(但我遇到了上一段中提到的问题)。
我是 Python 的新手,我来自另一种语言 (C#),所以如果这很微不足道,我很抱歉。
您将如何解决这个问题?
最佳答案
texts = [ "The lion (Panthera leo) ...", "Panthera ...", "..." ]
keywords = ['cat', 'lion', 'panthera', 'family']
# gives the count of `word in text`
def matches(text):
return sum(word in text.lower() for word in keywords)
# or inline that helper function as a lambda:
# matches = lambda text:sum(word in text.lower() for word in keywords)
# print the one with the highest count of matches
print max(texts, key=matches)
关于Python:如何在字符串列表中查找匹配最多的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9708374/