In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what is the most high-frequency module in python, which is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Today we will continue to introduce the second important type under the collections module, defaultdict.
Defaultdict
Defaultdict is a subclass of the dict type, and as its name suggests, you can assign default values to key when initializing. What does that mean? Just look at the code.
If it is an ordinary dict object, an error will be reported when accessing a key that does not exist:
> > raw_d = dict ()
> raw_d ['a']
NameError: name 'raw' is not defined
With defaultdict, you can give a default initial value to a key that does not exist, for example:
The default value is a dictionary with an empty list
D = defaultdict (list)
> d ['a']
[]
Dictionary with a default value of 0
> > d = defaultdict (int)
> d ['a']
0
The parameter received by the defaultdict function is a callable object, which you can simply understand as a function object. Int,list is also a function. What if I want the default value to be 1 instead of 0? You can use anonymous functions
> d = defaultdict (lambda: 1)
> d ['a']
one
You can also define a function yourself.
From collections import defaultdict
Def v2 ():
Return 2
D = defaultdict (v2)
Print (d ["a"]) # 2
Finally, talk about the usage scenario of defaultdict to make your code more pythonic.
Process the following data and put the same values in a group
Data = [("p", 1), ("p", 2), ("p", 3)
("h", 1), ("h", 2), ("h", 3)]
Become
Result= {'packs: [1,2,3],' hackers: [1,2,3]}
Think about how to do it for yourself. If you use defaultdict to deal with it, the code will be elegant.
From collections import defaultdict
# initialize a dictionary with an empty list by default
Result = defaultdict (list)
Data = [("p", 1), ("p", 2), ("p", 3)
("h", 1), ("h", 2), ("h", 3)]
# put the groups with the same value in a key
For (key, value) in data:
Result [key] .append (value)
Print (result)
> defaultdict (, {'paired: [1, 2, 3],' hackers: [1, 2, 3]}) the above is what is the most frequent module in python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.