Python learning tips about using the default behavior of dictionaries

Source: Internet
Author: User
This article mainly introduces the Python learning tips of the use of the dictionary of the default behavior of the relevant information, the text is described in very detailed, for everyone has a certain reference learning value, the need for friends below to see it together.

This article is about Python using the dictionary of the default behavior of the relevant content, share it for everyone to refer to the study, below to see the detailed introduction:

Typical code 1:

From collections import Defaultdict   if name = = ' main ':  data = defaultdict (int)  data[0] + = 1  print (data)

Output 1:

Defaultdict (<type ' int ';, {0:1})

Typical Code 2:

If name = = ' main ':  data = {' K ': 1}  data.setdefault (' k ', ' + ')  data.setdefault (' K1 ', -100)  print (data)

Output 2:

{' K ': 1, ' K1 ':-100}

Application Scenarios:

Typical application scenarios for code 1:

When writing some statistical code, always need to count the number of keys, with a dictionary to store the results of the count, if you use a classic dictionary, then we need to manually write code to determine whether the corresponding key exists, if it does not exist also need to be stored in this dictionary, and then initialize it to 0 And with the Defaultdict data type, we can directly specify a factory function to produce default values for us, typically using the built-in int function in code 1 and, of course, the anonymous function defined by the lambda expression.

Typical application scenarios for code 2:

For a dictionary, if we only want to preserve the value that each key is specified for the first time, if the traditional method data[' K ']= ' V ', we need to determine whether the corresponding key is already in the dictionary, and then decide whether or not it will be able to set the value of the key, and we can use the Dict SetDefault method to avoid this judgment and implement this function in a more concise way.

Benefits of:

1. The SetDefault method in scenario 2 is optimized for the implementation of the Python interpreter and is generally more efficient than the Python code that writes the same function

2. Both of these default scenarios make the code more compact, logically, and more efficient when reading code

Other instructions:

1. Defaultdict type, can receive many types, built-in list, set, Dict can be used directly, using lambda anonymous function, you can use your own defined type

Summarize

Related Article

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.