[Python-tools]defaultdict's usage scenario

Source: Internet
Author: User

The Python standard library has a lot of collections on the data structures of collection types, which can be a lot of convenience when we use collections, and it's good to see more.

Defaultdict is one of the methods to add the default type to the dictionary value element, which has been seen before but is not paid attention to.


First is the first example of the major articles:

Import collections as Colldef default_factory ():    return ' default value ' d = coll.defaultdict (default_factory, foo= ' Bar ') print ' d: ', Dprint ' foo=> ', d[' foo ']print ' foo=> ', d[' bar '   the element #key为 ' bar ' does not exist, there will be a default value
The output is this:
D:defaultdict (<function default_factory at 0x022e6e70>, {' foo ': ' Bar '}) foo=> barfoo=> default value

Conclusion: You can see here 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 a comparison:

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

Dict_set1 = {} #如果不知道这个字段中key有没有, you need to first determine if ' key ' not in Dict_set1:    Dict_set1 = set () dict_set1[' key '].add (' 111 ') dict_ set1[' key '].add (' $ ') print Dict_set1

If you use Defaultdict, that's it.

Dict_set = Coll.defaultdict (set) dict_set[' key '].add (' ") dict_set[' key '].add (' 111 ') print Dict_set

The advantage is that you do not need to do a set initialization of this judgment.


Two small cases of use

SS = ' 1111222233334444 ' dict_int = coll.defaultdict (int) for s in SS:    Dict_int[s] + = 1print dict_int "' This example of an official document can see the brevity of this notation 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 statistic-like data operation.

This article is derived from "Orangleliu notebook " Blog, be sure to keep this source http://blog.csdn.net/orangleliu/article/details/38669867


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.