How does collections.defaultdict work?

前端 未结 15 2291
离开以前
离开以前 2020-11-22 12:50

I\'ve read the examples in python docs, but still can\'t figure out what this method means. Can somebody help? Here are two examples from the python docs

>         


        
15条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:58

    defaultdict

    "The standard dictionary includes the method setdefault() for retrieving a value and establishing a default if the value does not exist. By contrast, defaultdict lets the caller specify the default(value to be returned) up front when the container is initialized."

    as defined by Doug Hellmann in The Python Standard Library by Example

    How to use defaultdict

    Import defaultdict

    >>> from collections import defaultdict
    

    Initialize defaultdict

    Initialize it by passing

    callable as its first argument(mandatory)

    >>> d_int = defaultdict(int)
    >>> d_list = defaultdict(list)
    >>> def foo():
    ...     return 'default value'
    ... 
    >>> d_foo = defaultdict(foo)
    >>> d_int
    defaultdict(, {})
    >>> d_list
    defaultdict(, {})
    >>> d_foo
    defaultdict(, {})
    

    **kwargs as its second argument(optional)

    >>> d_int = defaultdict(int, a=10, b=12, c=13)
    >>> d_int
    defaultdict(, {'a': 10, 'c': 13, 'b': 12})
    

    or

    >>> kwargs = {'a':10,'b':12,'c':13}
    >>> d_int = defaultdict(int, **kwargs)
    >>> d_int
    defaultdict(, {'a': 10, 'c': 13, 'b': 12})
    

    How does it works

    As is a child class of standard dictionary, it can perform all the same functions.

    But in case of passing an unknown key it returns the default value instead of error. For ex:

    >>> d_int['a']
    10
    >>> d_int['d']
    0
    >>> d_int
    defaultdict(, {'a': 10, 'c': 13, 'b': 12, 'd': 0})
    

    In case you want to change default value overwrite default_factory:

    >>> d_int.default_factory = lambda: 1
    >>> d_int['e']
    1
    >>> d_int
    defaultdict( at 0x7f34a0a91578>, {'a': 10, 'c': 13, 'b': 12, 'e': 1, 'd': 0})
    

    or

    >>> def foo():
    ...     return 2
    >>> d_int.default_factory = foo
    >>> d_int['f']
    2
    >>> d_int
    defaultdict(, {'a': 10, 'c': 13, 'b': 12, 'e': 1, 'd': 0, 'f': 2})
    

    Examples in the Question

    Example 1

    As int has been passed as default_factory, any unknown key will return 0 by default.

    Now as the string is passed in the loop, it will increase the count of those alphabets in d.

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

    Example 2

    As a list has been passed as default_factory, any unknown(non-existent) key will return [ ](ie. list) by default.

    Now as the list of tuples is passed in the loop, it will append the value in the d[color]

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

提交回复
热议问题