In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the get () function in the python dictionary. It is very detailed and has a certain reference value. Friends who are interested must finish it!
The 1.get () function uses keys to get values
In the past, when learning python, the common way to get worthwhile was to directly
Print (slave [key])
But in this method, a KeyError type error will be returned when the key does not exist in the dictionary. In this case, you can use the get () function to get the value by using the key.
Print (dict.get (key))
When operating with the get () function, a None is returned when the input key does not exist in the dictionary, so that the program runs without an exception.
two。 Count the number of elements in the list by using dictionaries
For example, there is a list that allows you to count the number of occurrences of some elements in the list.
Ls= ['aa','b','c','ddd','aa']
Count the number of occurrences of each element in the list:
Cou= {} # create an empty dictionary for i in ls: cou [I] = cou.get (iPower0) + 1 # and call it an assignment statement of get. The purpose is to create a new dictionary key value pair''assignment statement code equivalent to cou [I] = 0 cou [I} = coul [I] + 1' 'print (cou)
Output result:
{'aa': 2,' baked: 1, 'cased: 1,' ddd': 1}
The get () function has two parameters here, the first is to determine the key to be assigned, and the second is to assign an initial value to the key, but the get assignment statement is actually needed to assign the key (why is this described below)
For example, I now add this statement to the code.
Cou.get ('baked pamphlet 10) # is called the initial value statement.
The formulation of the initial value statement itself has no effect on the result, because there is no actual assignment statement operation on the key (I feel that the initial value statement is drawn up because the key to be assigned is not found in the dictionary. Because there is no key to be assigned at all, so the assignment fails, that is, it is equivalent to a function that does not return a value, and even if the function is assigned a parameter, it does not return any result.)
Note: the get () function is only valid for the first time as a key assignment statement (non-specified assignment statement, depending on the comments in the code box). For example, when the get () function uses the get assignment statement for the 'aa' key for the first time and then uses the get assignment statement for the first time, the key value is still the result of the first assignment operation. The following is the test code:
Ls= ['aa','b','c','ddd','aa'] cou= {} # create an empty dictionary for i in ls: cou [I] = cou.get (iMagin0) + 1 # functional logic below to explain that cou [' aa'] = cou.get ('aa',10) # function is the same as print (cou) when the previous code is executed for the second time
Here we focus on the fourth line of code: coul [I] = cou.get (iPhone0) + 1
The first line of code is logically executed twice, and the function of the get statement is different each time:
For the first time: coul [I] = cou.get (iJing 0) + 1 (iresume aa')
At this point, the function of the get statement is to assign an initial value, that is, to set the initial value of the key 'aa' to 0 and then add 1
The second time: coul [I] = cou.get (iJing 0) + 1 (iresume aa')
Because the get statement has already appeared as an assignment statement once, the assignment function is invalid when the statement is executed again, that is, the second parameter in the get statement is no longer valid for the key 'aa'. At this time, the function of the get statement is the function introduced in the first part of the article, so this statement can be equivalent to
Cou [I] = cou.get (I) + 1
That is,
Cou [I] = coul [I] + 1
The output is the same as the previous code:
{'aa': 2,' baked: 1, 'cased: 1,' ddd': 1}
When a get assignment statement is used alone, if the key appears for the first time, the effect is the same as that of the assignment statement (the same effect as creating a new key value through a for loop in an empty dictionary)
Cou ['e'] = cou.get (ePower10)
Output result:
```python
{'aa': 10,' breadth: 1 aa': 'cession: 1 recorder' ddd': 1 recorder 10}
Incidentally, if you want to directly change the value of the key in the dictionary, you can use the following methods:
The assignment statement cou ['aa'] = 10 # has a mandatory print (cou)
Output result:
{'aa': 10,' baked: 1, 'cased: 1,' ddd': 1}
Add: the difference between the key and the key
Both dict.get (key) and key [key] can get the corresponding key value when the key value exists. But when using slave [key], key must exist, otherwise an error will be reported. But the key in dict.get [key] may not exist, because the get method has a default parameter None, which returns None when key does not exist.
These are all the contents of the article "how to use the get () function in the python Dictionary". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.