Python learning tips: use the default behavior of dictionaries, and python tips

Source: Internet
Author: User

Python learning tips: use the default behavior of dictionaries, and python tips

This article describes Python's usage of the dictionary's default behavior and shares it with you for your reference. Let's take a look at the details below:

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', 100)  data.setdefault('k1', -100)  print(data) 

Output 2:

{'k': 1, 'k1': -100} 

Application scenarios:

Application scenarios of typical code 1:

When writing some statistical code, you always need to count the number of keys and use a dictionary to store the counting results. If you use a classic dictionary, every time, we need to manually write code to determine whether the corresponding key exists. If it does not exist, we need to store it in this dictionary and initialize it to 0. Instead, we use the defaultdict data type, we can directly specify a factory function to generate default values for us. Typical code 1 uses the built-in int function, and of course it can also be an anonymous function defined by lambda expressions.

Application scenarios of typical code 2:

For a dictionary, if we only want to keep the value specified for each key for the first time, if we use the traditional method data ['K'] = 'V, we need to determine whether the corresponding key exists in the dictionary every time before deciding whether to set the value of this key. 2. Using the setdefault method of dict, we can avoid this judgment, implement this function in a more concise way.

Benefits:

1. The setdefault method in scenario 2 is optimized in the implementation of the Python interpreter. Generally, it is more efficient to execute Python code with the same functions as the one you write.

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

Other Instructions:

1. The defaultdict type can receive many types. The built-in list, set, and dict functions can be used directly. lambda anonymous functions can be used to define their own types.

Summary

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

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.