Collections.defaultdict与普通字典的区别

807

我已经阅读了 Python 文档中的示例,但仍然无法理解这个方法的含义。有人可以帮忙吗?以下是 Python 文档中的两个示例:

>>> from collections import defaultdict

>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
...     d[k] += 1
...
>>> d.items()
dict_items([('m', 1), ('i', 4), ('s', 4), ('p', 2)])

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

参数intlist是用于什么的?


41
顺便提一句,根据你的使用情况,请别忘了在填充完defaultdict后将其设置为只读模式并冻结它,方法是将default_factory = None。参考这个问题 - Asclepius
1
参见:https://dev59.com/x2Qm5IYBdhLWcg3w8iuD - dreftymac
16个回答

7
简而言之: defaultdict(int) - 参数 int 表示值将是 int 类型。 defaultdict(list) - 参数 list 表示值将是 list 类型。

4

如果没有使用defaultdict,你可能可以给未见过的键赋值,但是你不能修改它。例如:

import collections
d = collections.defaultdict(int)
for i in range(10):
  d[i] += i
print(d)
# Output: defaultdict(<class 'int'>, {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9})

import collections
d = {}
for i in range(10):
  d[i] += i
print(d)
# Output: Traceback (most recent call last): File "python", line 4, in <module> KeyError: 0

4

我认为最好用它来代替switch case语句。假设我们有以下的switch case语句:

option = 1

switch(option) {
    case 1: print '1st option'
    case 2: print '2nd option'
    case 3: print '3rd option'
    default: return 'No such option'
}

Python中没有可用的switch case语句。我们可以通过使用defaultdict来实现相同的功能。

from collections import defaultdict

def default_value(): return "Default Value"
dd = defaultdict(default_value)

dd[1] = '1st option'
dd[2] = '2nd option'
dd[3] = '3rd option'

print(dd[4])    
print(dd[5])    
print(dd[3])

它会打印出:

Default Value
Default Value
3rd option

在上面的代码段中,dd没有键4或5,因此它打印出我们在帮助函数中配置的默认值。这比原始字典要好得多,因为如果键不存在,会抛出一个KeyError。从这可以看出,defaultdict更像是一个开关语句,我们可以避免复杂的if-elif-elif-else块。
还有一个很好的例子来自这个网站,给我留下了深刻的印象:
>>> from collections import defaultdict
>>> food_list = 'spam spam spam spam spam spam eggs spam'.split()
>>> food_count = defaultdict(int) # default value of int is 0
>>> for food in food_list:
...     food_count[food] += 1 # increment element's value by 1
...
defaultdict(<type 'int'>, {'eggs': 1, 'spam': 7})
>>>

如果我们尝试访问除了eggsspam之外的任何项,我们将得到一个计数为0的结果。


1
标准字典包括方法 setdefault() 用于检索值并在该值不存在时建立默认值。相比之下,defaultdict 允许调用者在容器初始化时预先指定默认值。
import collections

def default_factory():
    return 'default value'

d = collections.defaultdict(default_factory, foo='bar')
print 'd:', d
print 'foo =>', d['foo']
print 'bar =>', d['bar']

只要所有键都具有相同的默认值,这种方法就可以很好地发挥作用。如果默认值是用于聚合或累加值的类型(例如列表、集合甚至整数),则它尤其有用。标准库文档包括多个使用defaultdict的示例。
$ python collections_defaultdict.py

d: defaultdict(<function default_factory at 0x100468c80>, {'foo': 'bar'})
foo => bar
bar => default value

0
#dictinary and defaultdict

normaldictionary=dict()
print(type(normaldictionary))
#print(normaldictionary["keynotexisit"])
#Above normal dictionary give an error as key not present

from collections import defaultdict
defaultdict1=defaultdict()
print(type(defaultdict1))
#print(defaultdict1['keynotexisit'])
######################################

from collections import defaultdict
default2=defaultdict(int)
print(default2['keynotexist'])

https://msatutorpy.medium.com/different-between-dictionary-and-defaultdictionary-cb215f682971


-10

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接