【发布时间】:2018-11-09 15:00:28
【问题描述】:
我有两个列表:app 和 topic_list。在app 中,有很多应用程序,在topic_list 中,我存储了主题中的每个单词。现在我想知道哪三个应用程序在给定主题中出现次数最多。我的问题是count_top_3 中的所有计数器都是 1,它们不应该是。
count_top_3 = {} #create a dictionary to store the times that the word occurs
for words in app:
if words in topic_list:
if words not in count_top_3:
count_top_3[words] = 1
else:
count_top_3[words] += 1
print(count_top_3)
编辑:
应用列表如下:
['Google Maps',
'Facebook for Android',
'Pandora?? internet radio',
'Zedge Ringtones & Wallpapers',
'Advanced Task Killer',
'Twitter',
'Tiny Flashlight + LED',
'GO SMS Pro', ...]
topic_list 看起来像:
['Call Blocker X',
'FNB Connect Phone',
'LazyDroid Web Desktop',
'Private Space Free(SMS & Call)',
'Private Space Free',
'Super Call Blocker',
'Fast Society', ...]
这是我的字典输出:
{
'Google Maps': 1,
'Facebook for Android': 1,
'Pandora?? internet radio': 1,
'Zedge Ringtones & Wallpapers': 1,
'Advanced Task Killer': 1,
'Twitter': 1,
'Tiny Flashlight + LED': 1,
'GO SMS Pro': 1,
'The Weather Channel': 1,
'Shazam': 1,
'Lookout Security & Antivirus': 1,
'YouTube': 1,
'Dictionary.com': 1,
'TuneIn Radio': 1,
'Movies': 1,
'ColorNote Notepad Notes': 1,
'Antivirus Free': 1,
'Bible': 1,
'TiKL': 1...}
其实key的值不应该都是1,我想统计一下这些app在topic_list中出现的次数
【问题讨论】:
-
请提供示例输入和示例输出。
-
我在下面编辑它,谢谢!
-
这些示例的输出是什么?
-
如果您添加输入(
app和topic_list是什么)、您期望的输出(count_top_3应该是什么样子)以及您的实际得到(print (count_top_3)显示的内容)。否则我们无法帮助您解决此问题 -
我又编辑了!谢谢!
标签: python dictionary counter