Python collections Defaultdict

Source: Internet
Author: User

class_counts = defaultdict (int)

First, about Defaultdict

In Python there is a module collections, which is interpreted as a data type container module. There is a collections.defaultdict () which is often used.

Example:

 from Import  = defaultdict (int) a[1] = 1a["b"]print " a[' a ']== ", a["a"]print A
----------
a[' a ']== 0
Defaultdict (<type ' int '), {' A ': 0, 1:1, ' B ': 0})

The result is easy to see, default (int) creates a similar dictionary object, where any of the values is an instance of int, and even if it is a nonexistent key, D[key] has a default value, which is the default value of int () 0.

Here Defaultdict (function_factory) constructs an object similar to dictionary, where the value of keys is determined by itself, but the type of values is the class instance of Function_factory, and has a default value.

Second, other types

--------------------------2016-7-30 08:15:12--

Source: Use and difference of "1" Python collections.defaultdict () and Dict

Python collections Defaultdict

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.