【问题标题】:Counting frequencies in two lists, Python计算两个列表中的频率,Python
【发布时间】:2017-01-27 12:18:26
【问题描述】:

我是 python 编程的新手,所以请耐心等待我的新手问题...

我有一个初始列表 (list1) ,我已经清理了重复项并最终得到了一个列表,其中每个值只有一个 (list2):

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16],

list2 = [13, 19, 2, 16, 6, 5, 20, 21]

我想要计算“list2”中的每个值出现在“list1”中的次数,但我不知道如何做到这一点而不会弄错。

我正在寻找的输出类似于:

数字 13 在 list1 中表示 1 次。 ........ 数字 16 在 list1 中出现了 2 次。

【问题讨论】:

标签: python list frequency counting


【解决方案1】:

在技术术语中,list 是“对象”的“类型”。 Python 有许多内置类型,如字符串 (str)、整数 (int) 以及其他一些可以在 google 上轻松找到的类型。这很重要的原因是因为每种对象类型都有自己的“方法”。您可以将这些方法视为完成常见编程任务并使您的生活更轻松的功能。

计算列表中出现的次数是常见编程任务的一个示例。我们可以使用count() 方法来完成它。例如,统计 13 在 list1 中出现的次数:

count_13 = list1.count(13)

我们还可以使用 for 循环来遍历整个列表:

for x in list2:
    print(list1.count(x)) #This is for python version 3 and up

或者对于早于 3 的 python 版本:

for x in list2:
    print list1.count(x)

【讨论】:

    【解决方案2】:
    visited = []
    for i in list2:
      if i not in visited:
        print "Number", i, "is presented", list1.count(i), "times in list1"
        visited.append(i)
    

    【讨论】:

      【解决方案3】:

      您无需删除重复项。当您自动添加到字典时,重复项将被视为单个值。

      list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
      counts = {s:list1.count(s) for s in list1}
      print counts
      
      {2: 1, 5: 1, 6: 1, 13: 4, 16: 2, 19: 3, 20: 2, 21: 1}
      

      【讨论】:

        【解决方案4】:

        你也可以使用运算符

        >>> list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16],
        >>> list2 = [13, 19, 2, 16, 6, 5, 20, 21]
        
        >>> import operator
        >>> for s in list2:
        ...    print s, 'appearing in :',  operator.countOf(list1, s)
        

        【讨论】:

          【解决方案5】:

          最简单最容易理解,no-magic-approach就是创建一个对象(关联数组),只计算里面的数字列表1:

          list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
          
          frequency_list = {}
          
          for l in list1:
              if l in frequency_list:
                  frequency_list[l] += 1
              else:
                  frequency_list[l] = 1
          
          print(frequency_list)
          

          打印出来:

          {
              16: 2,
              2: 1,
              19: 3,
              20: 2,
              5: 1,
              6: 1,
              13: 4,
              21: 1
          }
          

          意思是 16 有两次,2 有一次...

          【讨论】:

          • 一个更紧凑的版本是for item in list1: frequency_list[item] = frequency_list.get(item, 0) + 1。我不知道你是否认为那是“神奇的”,但我确实更喜欢它用于干净的代码。我想这确实需要更多的理解,所以也许只是一个意见问题:)
          • @roganjosh:很好。我只是认为一个新手问题需要一个新手答案;-)
          • 多么有趣。在 24:25 左右在vid by Hettinger 中看到我的建议后,我开始使用它。我决定进行测试,您的方法始终以合理的幅度更快......
          【解决方案6】:

          最简单的方法是使用计数器:

          from collections import Counter
          list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
          c = Counter(list1)
          print(c)
          

          给予

          Counter({2: 1, 5: 1, 6: 1, 13: 4, 16: 2, 19: 3, 20: 2, 21: 1})
          

          因此,您可以使用用于访问 dicts 的相同语法访问表示项目及其出现的计数器的键值对:

          for k, v in c.items():
              print('- Element {} has {} occurrences'.format(k, v))
          

          给予:

          - Element 16 has 2 occurrences
          - Element 2 has 1 occurrences
          - Element 19 has 3 occurrences
          - Element 20 has 2 occurrences
          - Element 5 has 1 occurrences
          - Element 6 has 1 occurrences
          - Element 13 has 4 occurrences
          - Element 21 has 1 occurrences
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-10
            • 2013-12-28
            相关资源
            最近更新 更多