我已经阅读了python文档中的示例,但仍然不明白这个方法是什么意思。有人能帮帮我吗?下面是python文档中的两个例子

>>> from collections import defaultdict

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

and

>>> 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])]

参数int和list是干什么用的?


当前回答

由于这个问题是关于“它是如何工作的”,一些读者可能想了解更多的具体细节。具体来说,问题中的方法是__missing__(key)方法。参见:https://docs.python.org/2/library/collections.html#defaultdict-objects。

更具体地说,这个答案展示了如何以一种实用的方式使用__missing__(key): https://stackoverflow.com/a/17956989/1593924

为了澄清'callable'的含义,这里有一个交互式会话(来自2.7.6,但也适用于v3):

>>> x = int
>>> x
<type 'int'>
>>> y = int(5)
>>> y
5
>>> z = x(5)
>>> z
5

>>> from collections import defaultdict
>>> dd = defaultdict(int)
>>> dd
defaultdict(<type 'int'>, {})
>>> dd = defaultdict(x)
>>> dd
defaultdict(<type 'int'>, {})
>>> dd['a']
0
>>> dd
defaultdict(<type 'int'>, {'a': 0})

这是defaultdict最典型的用法(除了毫无意义地使用x变量)。你可以使用0作为显式默认值,但不能使用简单的值:

>>> dd2 = defaultdict(0)

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    dd2 = defaultdict(0)
TypeError: first argument must be callable

相反,下面的函数可以工作,因为它传递了一个简单的函数(它动态地创建了一个无参数且总是返回0的无名函数):

>>> dd2 = defaultdict(lambda: 0)
>>> dd2
defaultdict(<function <lambda> at 0x02C4C130>, {})
>>> dd2['a']
0
>>> dd2
defaultdict(<function <lambda> at 0x02C4C130>, {'a': 0})
>>> 

并且使用不同的默认值:

>>> dd3 = defaultdict(lambda: 1)
>>> dd3
defaultdict(<function <lambda> at 0x02C4C170>, {})
>>> dd3['a']
1
>>> dd3
defaultdict(<function <lambda> at 0x02C4C170>, {'a': 1})
>>> 

其他回答

defaultdict意味着如果在字典中没有找到一个键,那么不会抛出KeyError,而是创建一个新条目。这个新条目的类型由defaultdict的参数给出。

例如:

somedict = {}
print(somedict[3]) # KeyError

someddict = defaultdict(int)
print(someddict[3]) # print int(), thus 0

defaultdict的行为可以很容易地使用dict模仿。在每次调用中设置default而不是d[key]。

换句话说,代码:

from collections import defaultdict

d = defaultdict(list)

print(d['key'])                        # empty list []
d['key'].append(1)                     # adding constant 1 to the list
print(d['key'])                        # list containing the constant [1]

等价于:

d = dict()

print(d.setdefault('key', list()))     # empty list []
d.setdefault('key', list()).append(1)  # adding constant 1 to the list
print(d.setdefault('key', list()))     # list containing the constant [1]

唯一的区别是,使用defaultdict时,列表构造函数只被调用一次,而使用dict时。Setdefault会更频繁地调用列表构造函数(但如果确实需要,代码可能会被重写以避免这种情况)。

有些人可能会说这是出于性能考虑,但这个话题是一个雷区。例如,这篇文章展示了使用defaultdict并不会带来很大的性能提升。

在我看来,defaultdict是一个给代码带来更多困惑而不是好处的集合。对我没用,但别人可能不这么认为。

在以下情况下,defaultdict也会引发keyerror:

from collections import defaultdict
d = defaultdict()
print(d[3]) #raises keyerror

总是记得给defaultdict参数

d = defaultdict(int)

Usually, a Python dictionary throws a KeyError if you try to get an item with a key that is not currently in the dictionary. The defaultdict in contrast will simply create any items that you try to access (provided of course they do not exist yet). To create such a "default" item, it calls the function object that you pass to the constructor (more precisely, it's an arbitrary "callable" object, which includes function and type objects). For the first example, default items are created using int(), which will return the integer object 0. For the second example, default items are created using list(), which returns a new empty list object.

我自己的2分:你也可以子类defaultdict:

class MyDict(defaultdict):
    def __missing__(self, key):
        value = [None, None]
        self[key] = value
        return value

这在非常复杂的情况下可以派上用场。