【发布时间】:2019-06-08 22:02:58
【问题描述】:
如何找出关键字列表在文章标题列表中出现的次数?我正在查看柜台,并通过了这个解决方案 (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。
【问题讨论】:
标签: python-3.x string list search counter