Python:Collections.Counter vs defaultdict(int) [英] Python: Collections.Counter vs defaultdict(int)

查看:215
本文介绍了Python:Collections.Counter vs defaultdict(int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些数据如下所示。

Suppose I have some data that looks like the following.

Lucy = 1
Bob = 5
Jim = 40
Susan = 6
Lucy = 2
Bob = 30
Harold = 6

我想组合1)删除重复键,并且2)添加这些重复键的值。这意味着我会得到键/值:

I want to combine 1) remove duplicate keys, and 2) add the values for these duplicate keys. That means I'd get the key/values:

Lucy = 3
Bob = 35
Jim = 40
Susan = 6
Harold = 6

推荐答案

Counter defaultdict(int)可以在这里工作,但是它们之间的区别很小:

Both Counter and defaultdict(int) can work fine here, but there are few differences between them:


  • Counter 支持您可以在 multiset 。所以,如果你想使用这些操作,然后去计数器。

  • Counter supports most of the operations you can do on a multiset. So, if you want to use those operation then go for Counter.

计数器不会在查询缺少的键时向dict添加新的键。所以,如果你的查询包含可能不存在于dict中的键,那么更好地使用 Counter

Counter won't add new keys to the dict when you query for missing keys. So, if your queries include keys that may not be present in the dict then better use Counter.

示例:

>>> c = Counter()
>>> d = defaultdict(int)
>>> c[0], d[1]
(0, 0)
>>> c
Counter()
>>> d
defaultdict(<type 'int'>, {1: 0})

示例:


  • 计数器还有一个名为 most_common 可以让您按照计数排序项目。要在 defaultdict 中获得相同的事情,您必须使用排序

  • Counter also has a method called most_common that allows you to sort items by their count. To get the same thing in defaultdict you'll have to use sorted.

示例:

>>> c = Counter('aaaaaaaaabbbbbbbcc')
>>> c.most_common()
[('a', 9), ('b', 7), ('c', 2)]
>>> c.most_common(2)          #return 2 most common items and their counts
[('a', 9), ('b', 7)]




  • 计数器还允许您从Counter对象创建一个元素列表。

    • Counter also allows you to create a list of elements from the Counter object.
    • 示例:

      >>> c = Counter({'a':5, 'b':3})
      >>> list(c.elements())
      ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b']
      

      所以,根据你想要的结果,你可以选择 Counter defaultdict(int)

      So, depending on what you want to do with the resulting dict you can choose between Counter and defaultdict(int).

      这篇关于Python:Collections.Counter vs defaultdict(int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆