【问题标题】:Item frequency count in PythonPython中的项目频率计数
【发布时间】:2010-10-27 23:07:15
【问题描述】:

假设我有一个单词列表,我想找出每个单词在该列表中出现的次数。

一个明显的方法是:

words = "apple banana apple strawberry banana lemon"
uniques = set(words.split())
freqs = [(item, words.split().count(item)) for item in uniques]
print(freqs)

但我觉得这段代码不太好,因为程序运行了两次单词表,一次是建立集合,第二次是计算出现次数。

当然,我可以编写一个函数来遍历列表并进行计数,但这不会像 Python 那样。那么,有没有更高效和 Pythonic 的方式呢?

【问题讨论】:

标签: python count frequency counting


【解决方案1】:

collections 模块中的Counter class 专门用于解决此类问题:

from collections import Counter
words = "apple banana apple strawberry banana lemon"
Counter(words.split())
# Counter({'apple': 2, 'banana': 2, 'strawberry': 1, 'lemon': 1})

【讨论】:

  • 根据stackoverflow.com/a/20308657/2534876,这在 Python3 上最快,但在 Python2 上慢。
  • 你知道是否有一个标志可以将其转换为百分比 freq_dict 吗?例如,'apple' : .3333 (2/6),
  • @Tommy total = sum(your_counter_object.values()) 然后freq_percentage = {k: v/total for k, v in your_counter_object.items()}
【解决方案2】:

defaultdict 来救援!

from collections import defaultdict

words = "apple banana apple strawberry banana lemon"

d = defaultdict(int)
for word in words.split():
    d[word] += 1

这在 O(n) 中运行。

【讨论】:

  • 这是一个非常古老的答案。请改用Counter
【解决方案3】:
freqs = {}
for word in words:
    freqs[word] = freqs.get(word, 0) + 1 # fetch and increment OR initialize

我认为这与 Triptych 的解决方案相同,但没有导入集合。也有点像 Selinap 的解决方案,但更易读恕我直言。几乎与 Thomas Weigel 的解决方案相同,但没有使用异常。

然而,这可能比使用集合库中的 defaultdict() 慢。由于该值被获取,递增然后再次分配。而不是仅仅增加。但是使用 += 可能在内部做同样的事情。

【讨论】:

    【解决方案4】:

    标准方法:

    from collections import defaultdict
    
    words = "apple banana apple strawberry banana lemon"
    words = words.split()
    result = defaultdict(int)
    for word in words:
        result[word] += 1
    
    print result
    

    Groupby oneliner:

    from itertools import groupby
    
    words = "apple banana apple strawberry banana lemon"
    words = words.split()
    
    result = dict((key, len(list(group))) for key, group in groupby(sorted(words)))
    print result
    

    【讨论】:

    • 复杂度有区别吗? groupby 是否使用排序?那么它似乎需要 O(nlogn) 时间?
    • 糟糕,下面的 Nick Presta 似乎指出 groupby 方法使用 O(nlogn)。
    【解决方案5】:

    如果你不想使用标准的字典方法(循环遍历列表并增加正确的 dict.key),你可以试试这个:

    >>> from itertools import groupby
    >>> myList = words.split() # ['apple', 'banana', 'apple', 'strawberry', 'banana', 'lemon']
    >>> [(k, len(list(g))) for k, g in groupby(sorted(myList))]
    [('apple', 2), ('banana', 2), ('lemon', 1), ('strawberry', 1)]
    

    它在 O(n log n) 时间内运行。

    【讨论】:

      【解决方案6】:

      没有默认字典:

      words = "apple banana apple strawberry banana lemon"
      my_count = {}
      for word in words.split():
          try: my_count[word] += 1
          except KeyError: my_count[word] = 1
      

      【讨论】:

      • 在我的测试中似乎比 defaultdict 慢
      • 用空格分割是多余的。此外,您应该使用 dict.set_default 方法而不是 try/except。
      • 速度慢很多,因为您使用的是异常。几乎任何语言的例外都是非常昂贵的。避免将它们用于逻辑分支。查看我的解决方案以获取几乎相同的方法,但不使用异常:stackoverflow.com/questions/893417/…
      【解决方案7】:
      user_input = list(input().split(' '))
      
      for word in user_input:
      
          print('{} {}'.format(word, user_input.count(word)))
      

      【讨论】:

        【解决方案8】:
        words = "apple banana apple strawberry banana lemon"
        w=words.split()
        e=list(set(w))       
        word_freqs = {}
        for i in e:
            word_freqs[i]=w.count(i)
        print(word_freqs)   
        

        希望这会有所帮助!

        【讨论】:

          【解决方案9】:

          你不能只用计数吗?

          words = 'the quick brown fox jumps over the lazy gray dog'
          words.count('z')
          #output: 1
          

          【讨论】:

          • 该问题已使用“count”,并要求更好的替代方案。
          【解决方案10】:

          我碰巧做了一些 Spark 练习,这是我的解决方案。

          tokens = ['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog']
          
          print {n: float(tokens.count(n))/float(len(tokens)) for n in tokens}
          

          **#上面的输出**

          {'brown': 0.16666666666666666, 'lazy': 0.16666666666666666, 'jumps': 0.16666666666666666, 'fox': 0.16666666666666666, 'dog': 0.16666666666666666, 'quick': 0.16666666666666666}
          

          【讨论】:

            【解决方案11】:
            list = input()  # Providing user input passes multiple tests
            text = list.split()
            
            for word in text:
                freq = text.count(word) 
                print(word, freq)
            

            【讨论】:

              【解决方案12】:

              使用 reduce() 将列表转换为单个字典。

              from functools import reduce
              
              words = "apple banana apple strawberry banana lemon"
              reduce( lambda d, c: d.update([(c, d.get(c,0)+1)]) or d, words.split(), {})
              

              返回

              {'strawberry': 1, 'lemon': 1, 'apple': 2, 'banana': 2}
              

              【讨论】:

                【解决方案13】:

                下面的答案需要一些额外的循环,但它是另一种方法

                def func(tup):
                    return tup[-1]
                
                
                def print_words(filename):
                    f = open("small.txt",'r')
                    whole_content = (f.read()).lower()
                    print whole_content
                    list_content = whole_content.split()
                    dict = {}
                    for one_word in list_content:
                        dict[one_word] = 0
                    for one_word in list_content:
                        dict[one_word] += 1
                    print dict.items()
                    print sorted(dict.items(),key=func)
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-06-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-12-03
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-10-14
                  相关资源
                  最近更新 更多