python-3.x - 查找关键字列表在文章标题列表中出现的次数
问题描述
我将如何找出关键字列表在文章标题列表中出现的次数?我正在查看计数器,并通过了这个解决方案(如何计算列表项的出现次数?),它非常适合比较单个单词,但如果您的列表中的一个具有您需要搜索的气味,则不是。例子:
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。
解决方案
这是一种可能性:
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 可以使其更简洁,但到目前为止这似乎有效。
推荐阅读
- javascript - 如何制作具有每月间隔的动态日期时间范围的高图
- java - 如何从 Java 中的一组数字中找到完美平方?
- reactjs - 开玩笑说 SyntaxError: Unexpected token export - React, Material.io
- sql-server - SQL Server 将 Varchar 转换为日期时间 SQL Server
- python - Python urllib.request.urlopen:AttributeError:'bytes'对象没有属性'data'
- php - PHP字符串到数组 - 我的字符串卡住了
- python - python处理带有命名空间的xml字符串
- c - C 中的位和数组 - 编程新手
- react-native - 为什么这个 react-native-reanimated 代码块不是每一帧都运行?
- google-apps-script - 简单的数据拼贴电子表格