【问题标题】:dictionary count how many times the key word occur in the articles字典计算关键词在文章中出现的次数
【发布时间】:2018-11-09 15:00:28
【问题描述】:

我有两个列表:apptopic_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中出现的次数

【问题讨论】:

  • 请提供示例输入和示例输出。
  • 我在下面编辑它,谢谢!
  • 这些示例的输出是什么?
  • 如果您添加输入(apptopic_list 是什么)、您期望的输出(count_top_3 应该是什么样子)以及您的实际得到(print (count_top_3) 显示的内容)。否则我们无法帮助您解决此问题
  • 我又编辑了!谢谢!

标签: python dictionary counter


【解决方案1】:

您的问题是您只检查words 是否在topic_list 中,而不是它出现的次数(将布尔值映射到整数而不是 将一组布尔值映射到一个整数上),因此您只会得到 1 的值。您的问题的解决方案取决于 app 中的数据:

如果应用中的值是唯一的:

在这种情况下,您可以简单地切换用作索引的数据。因为,根据您的问题,topic_list 中的值不是唯一的,因此检查它们是否在 app 中可以让您在不对逻辑进行大改动的情况下对它们进行计数。

for words in topic_list:
    if words in app:
        if words not in count_top_3:
            count_top_3[words] = 1
        else:
            count_top_3[words] += 1
print(count_top_3) 

如果应用中的值不是唯一的:

在这种情况下,如果您尝试第一个解决方案,您将遇到当前问题,只是来自其他数据集。所以,你需要做的是统计wordstopic_list 中出现的次数:

for words in app:
    count = topic_list.count(words)
    if words not in count_top_3:
        count_top_3[words] = count
    else:
        count_top_3[words] += count

【讨论】:

    【解决方案2】:

    根据您的代码的工作方式,如果该应用多次在 app[] 中列出,则放置在 count_top_3 中的应用只会有一个 > 1 的计数器,因为您只是检查每个应用是否 topic_list,而不是 多少次 它在 topic_list 中。在您的示例输入中,app[] 中没有重复项。

    如果您的目标是计算每个应用在 topic_list 中出现的次数,您可以循环遍历 app[] 中的每个应用,然后对于每个应用,循环遍历 topic_list[] 中的每个主题,并计算出现次数主题匹配该应用程序,将结果存储在 count_top_3 中。您也可以为此使用 count() 方法。

    例子:

    for currentApp in app:
        count_top_3[currentApp] = topic_list.count(currentApp)
    

    【讨论】:

    • 嗨,我试过你的方法,但是我的字典的计数值都变成了 10。按照常识,计数器不应该相等,因为数据是数据集包含约 180,000 个来自 Android 的应用程序应用商店...
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多