寻找关键词列表在文章标题列表中出现多少次

时间:2019-01-14 21:42:25

标签: python-3.x string list search counter

如何找出文章标题列表中关键字列表出现的次数?我正在查看计数器,并得到了该解决方案(How can I count the occurrences of a list item?),它对于比较单个单词非常有用,但是如果您的列表中有一个需要搜索的香味就不能了。示例:

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。

1 个答案:

答案 0 :(得分:0)

这是一种可能性:

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使其更简洁,但是到目前为止,这似乎可行。