In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the defaultdict dictionary in python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Defaultdict is a subclass of the common dictionary dict. Created by the defaultdict () method of the collections library.
Defaultdict () requires that the argument passed in is the class name. (type type)
First of all, take the int class as an example
From collections import defaultdictdedict = defaultdict (int) print (dedict)
Unlike ordinary dictionaries, the feature is that when called with a key that does not exist in defaultdict, a KeyError error is not reported
Take the value of the'a 'key as an example:
Print (dedict ['a'])
The value of this key defaults to zero. This is the case where the defaultdict () parameter is int, and the default value varies with the parameter.
Defaultdict () requires that the argument passed in is the class name. (type type)
Pass in other classes to create defaultdict (). The effect is as follows:
From collections import defaultdictdedict1 = defaultdict (list) dedict2 = defaultdict (set) dedict3 = defaultdict (dict) dedict4 = defaultdict (tuple) dedict5 = defaultdict (int) dedict6 = defaultdict (float) dedict7 = defaultdict (str) # create a useless class class A (object): pass# input available! Dedict8 = defaultdict (A) print (dedict1 ['a']) print (dedict2 ['a']) print (dedict3 ['a']) print (dedict4 ['a']) print (dedict5 ['a']) print (dedict6 ['a']) print (dedict7 ['a']) print (dedict8 ['a'])
The running result of the program is as follows:
As you can see, the default value of a key that does not exist is the default instance object of the incoming class.
The default Boolean value is usually False, but not necessarily.
Whether it is False or not is related to the class, so the Boolean value passing through class An is True.
It is even possible to set a doll:
Dedict9 = defaultdict (defaultdict) print (dedict9 ['a'])
Defaultdict is often used to count the number of multiple target data and is stored in the defaultdict dictionary.
Examples are as follows:
From collections import defaultdictbag = ["cheese", "Bread", "apple", "Bread", "orange", "cheese", 'apple', "milk", "apple", "bananas", "milk"] count = defaultdict (int) for item in bag: count [item] + = 1print (count)
Thank you for reading! This is the end of the article on "how to use the defaultdict Dictionary in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.