在初始化时填充defaultdict

时间:2014-01-14 00:13:36

标签: python list-comprehension defaultdict generator-expression

如何让defaultdict的可调用工厂允许用理解填充它?我认为这可能不可能,但我想不出一个好理由?

>>> def foo(*args):
...     # TODO
...
>>> from collections import defaultdict
>>> thing = foo(defaultdict, int)
>>> d = thing((i, i*i) for i in range(3))
>>> d[2]
# should return 4
>>> d[-1]
# should return 0

2 个答案:

答案 0 :(得分:6)

defaultdict之后default_factory的所有参数都被视为dict的参数:

>>> defaultdict(int, [(i, i*i) for i in range(5)])
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4, 3: 9, 4: 16})

只需将理解传递给defaultdict并让它完成工作:

def defaultdict_factory_factory(default_factory):
    def defaultdict_factory(*args, **kwargs):
        return defaultdict(default_factory, *args, **kwargs)
    return defaultdict_factory

或使用functools.partial

def defaultdict_factory_factory(default_factory):
    return partial(defaultdict, default_factory)

答案 1 :(得分:5)

您是否只是在寻找defaultdict.update

>>> from collections import defaultdict
>>> thing = defaultdict(int)
>>> thing.update((i, i*i) for i in range(3))
>>> thing
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4})

你可以把它放到一个函数中。

>>> def initdefaultdict(type_, *args, **kwargs):
...     d = defaultdict(type_)
...     d.update(*args, **kwargs)
...     return d
... 
>>> thing = initdefaultdict(int, ((i, i+10) for i in range(3)))
>>> thing
defaultdict(<type 'int'>, {0: 10, 1: 11, 2: 12})
>>> thing[3]
0

或者为了满足您的原始要求,请返回一个功能:

>>> def defaultdictinitfactory(type_): # this is your "foo"
...     def createupdate(*args, **kwargs):
...             d = defaultdict(type_)
...             d.update(*args, **kwargs)
...             return d
...     return createupdate
... 
>>> f = defaultdictinitfactory(int) # f is your "thing"
>>> d = f((i, i*i) for i in range(3))
>>> d
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4})
>>>