【发布时间】:2012-03-14 18:57:46
【问题描述】:
我会尽量详细解释我需要什么:
我正在使用 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 个关键字(不管它说“猫”而不是“猫”,我只需要找到字符串中的文字关键字)
让我澄清一下,即使某些描述包含 'cat' 关键字 100 次(并且没有其他关键字),这也不会是赢家,因为我正在寻找包含最多的关键字,而不是最多的次数出现一个关键字。
现在,我正在遍历 rss 项目并“手动”执行,计算关键字出现的次数(但我遇到了上一段中提到的问题)。
我是 Python 的新手,我来自另一种语言 (C#),所以如果这很琐碎,我很抱歉。
您将如何解决这个问题?
【问题讨论】:
-
下面的答案都很棒,但要注意部分匹配(
concatenate是否算作cat的出现?)和大写(Cat算不算匹配?@987654326 怎么样? @?) -
是的,现在 'concatenate' 算作 'cat' 的出现,不必区分大小写。感谢您的警告。
标签: python string list rss string-matching