我已经阅读了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是干什么用的?


当前回答

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.

其他回答

defaultdict工具是Python集合类中的一个容器。它类似于通常的字典(dict)容器,但有一个区别:值字段的数据类型是在初始化时指定的。

例如:

from collections import defaultdict

d = defaultdict(list)

d['python'].append("awesome")

d['something-else'].append("not relevant")

d['python'].append("language")

for i in d.items():

    print i

这个打印:

('python', ['awesome', 'language'])
('something-else', ['not relevant'])

标准字典包含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']

只要所有键都具有相同的默认值,这种方法就可以很好地工作。如果默认值是用于聚合或累积值的类型,例如列表、集合甚至int,那么它会特别有用。标准库文档包含了以这种方式使用defaultdict的几个示例。

$ python collections_defaultdict.py

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

字典是一种方便的方式来存储数据,以便以后按名称(键)检索。键必须是唯一的、不可变的对象,通常是字符串。字典中的值可以是任何东西。对于许多应用程序,值是简单类型,如整数和字符串。

It gets more interesting when the values in a dictionary are collections (lists, dicts, etc.) In this case, the value (an empty list or dict) must be initialized the first time a given key is used. While this is relatively easy to do manually, the defaultdict type automates and simplifies these kinds of operations. A defaultdict works exactly like a normal dict, but it is initialized with a function (“default factory”) that takes no arguments and provides the default value for a nonexistent key.

defaultdict永远不会引发KeyError。任何不存在的键都将获得默认工厂返回的值。

from collections import defaultdict
ice_cream = defaultdict(lambda: 'Vanilla')

ice_cream['Sarah'] = 'Chunky Monkey'
ice_cream['Abdul'] = 'Butter Pecan'

print(ice_cream['Sarah'])
>>>Chunky Monkey

print(ice_cream['Joe'])
>>>Vanilla

下面是另一个关于如何使用defaultdict的例子,我们可以降低复杂性

from collections import defaultdict
# Time complexity O(n^2)
def delete_nth_naive(array, n):
    ans = []
    for num in array:
        if ans.count(num) < n:
            ans.append(num)
    return ans

# Time Complexity O(n), using hash tables.
def delete_nth(array,n):
    result = []
    counts = defaultdict(int)

    for i in array:
        if counts[i] < n:
            result.append(i)
            counts[i] += 1
    return result


x = [1,2,3,1,2,1,2,3]
print(delete_nth(x, n=2))
print(delete_nth_naive(x, n=2))

总之,当您需要字典,并且每个元素的值都应该以默认值开始时,请使用defaultdict。

这里有一个关于defaultdicts的很好的解释:http://ludovf.net/blog/python-collections-defaultdict/

基本上,参数int和list是传递的函数。记住,Python接受函数名作为参数。Int默认返回0,而list在使用括号调用时返回空列表。

在正常的字典中,如果在你的例子中我尝试调用d[a],我会得到一个错误(KeyError),因为只有键m, s, I和p存在,键a还没有初始化。但在defaultdict中,它接受函数名作为参数,当您尝试使用未初始化的键时,它只是调用您传入的函数并将其返回值分配为新键的值。

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

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

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