Python standard library Defaultdict module Usage Example

Source: Internet
Author: User

The Python standard library collections A number of extensions to the data structure of the collection type, which can be a lot of convenience when we use the collection, and it's good to see more.

Defaultdict is one of the ways to add a default type to the dictionary value element, but I've seen it before but didn't pay attention to how to use it, especially today.

First, the first example of a major article:

The code is as follows:

Import Collections as Coll

Def default_factory ():

Return ' default value '

D = coll.defaultdict (default_factory, foo= ' bar ')

print ' d: ', D

print ' foo=> ', d[' foo ']

print ' foo=> ', d[' bar ' #key为 ' bar ' element does not exist, there will be a default value

The output is like this:

The code is as follows:

D:defaultdict ( , {' foo ': ' Bar '})

Foo=> Bar

foo=> Default Value

Conclusion: Here you can see that when we take a key that is not in the dictionary, we automatically generate a value based on default_factory, similar to D.get (' bar ', ' default value ').

An example of contrast:

If the value of a map in a dictionary is a set, add two consecutive elements to the set, using the original Dict.

The code is as follows:

Dict_set1 = {}

#如果不知道这个字段中key有没有, you need to judge first.

If ' key ' not in Dict_set1:

dict_set1[' key '] = set ()

dict_set1[' key '].add (' 111 ')

dict_set1[' key '].add (' 000 ')

Print Dict_set1

If you use Defaultdict, that's it.

The code is as follows:

Dict_set = Coll.defaultdict (set)

dict_set[' key '].add (' 000 ')

dict_set[' key '].add (' 111 ')

Print Dict_set

The advantage is that there is no need to do the initialization of the set.

Two small cases of use

The code is as follows:

SS = ' 1111222233334444 '

Dict_int = coll.defaultdict (int)

For s in SS:

Dict_int[s] + + 1

Print Dict_int

'''''

This example of an official document can see the simplicity of this writing.

Https://docs.python.org/2/library/collections.html#collections.defaultdict

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

'''

This object works well when we do this data manipulation with statistical properties.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.