首页 > 解决方案 > 查找关键字列表在文章标题列表中出现的次数

问题描述

我将如何找出关键字列表在文章标题列表中出现的次数?我正在查看计数器,并通过了这个解决方案(如何计算列表项的出现次数?),它非常适合比较单个单词,但如果您的列表中的一个具有您需要搜索的气味,则不是。例子:

news_keywords = ['racism','ordeal','hero']
article_list = ['live: suspect in jayme closs case appears in court - cnn', 'woman who helped missing girl describes ordeal - cnn video', 'jayme closs found alive - cnn video', 'suspect in jayme closs case tried to kidnap her twice previously, complaint states', "trump keeps mum on king's comments while separately stoking racism", 'republicans are losing the shutdown blame game', 'government shutdown: live updates - cnnpolitics', "neighbors were 'armed and ready' if suspect in jayme closs kidnapping showed up", "investigators tracking down movements of jayme closs' kidnap suspect", 'sheriff says jayme closs is a hero after she freed herself from captivity and sought help']
a = [[x,article_list.count(x)] for x in set(news_keywords)]
print(a)

Desired output: [['ordeal', 1], ['racism', 1], ['hero', 1]]
Actual output: [['ordeal', 0], ['racism', 0], ['hero', 0]]

我可以将所有列表项组合成一个段落并执行某种搜索功能。但我很好奇我怎么能做到这一点,将所有文章标题保留在一个列表中,或者至少是一些可迭代的东西。

编辑。所以我最终这样做了:

def searchArticles():
    article_list = ['live: suspect in jayme closs case appears in court - cnn', 'woman who helped missing girl describes ordeal - cnn video', 'jayme closs found alive - cnn video', 'suspect in jayme closs case tried to kidnap her twice previously, complaint states', "trump keeps mum on king's comments while separately stoking racism", 'republicans are losing the shutdown blame game', 'government shutdown: live updates - cnnpolitics', "neighbors were 'armed and ready' if suspect in jayme closs kidnapping showed up", "investigators tracking down movements of jayme closs' kidnap suspect", 'sheriff says jayme closs is a hero after she freed herself from captivity and sought help']
    dump = ' '.join(article_list)
    dump_list = dump.split()
    a = [[x,dump_list.count(x)] for x in set(news_keywords)]
    print(dump_list)
    print(a)

但是如果还有其他想法,lmk。

标签: python-3.xstringlistsearchcounter

解决方案


这是一种可能性:

a = []

for x in set(news_keywords):
    a.append([x, sum([article_list[i].count(x) for i in range(len(article_list))])])

print(a)

当然,使用列表推导和 lambda 可以使其更简洁,但到目前为止这似乎有效。


推荐阅读