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/02 Report--
This article mainly shows you "what are the differences between defaultdict and dict in Python", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn "what are the differences between defaultdict and dict in Python".
I. description of the problem
What is the difference between the use of defauldict in the collections module and dict, and why we report an error when we use the key value in dict when it does not exist, and defaudict will not report an error? here is the answer.
II. Solutions
In order to solve the problems encountered.
Code example:
Import collections// refers to the collections module dic=collections.defaultdict (int) / / defines a dictionary using the defauldict in the module for num in range (10): dict [num] + = 1ram / assign print to the dictionary (dic)
Output:
Defaultdict (, {0: 1,1: 1,2: 1,3: 1,4: 1,5: 1,6: 1,7: 1,8: 1,9: 1})
You can see from the code that the defauldict () attribute in the collections module is referenced, a dictionary is defined, and the dictionary is assigned and added a key value. You can see that when there is no key value for dic at first, the dictionary returns a value of 1; similarly, what happens if you use dict directly?
Code example:
Dic=dict () / / defines a dictionary for num in range (10): Dico [num] + = 1 print / assignment print (dic)
Output:
Exception occurred: KeyError
0
File "C:\ Users\ Hasee\ Desktop\ where2go-python-test\ 1906101031 King Excellence\ Class\ ce_shi.py", line 81, in dic [num] + = 1
However, the output will report an error because the corresponding key value cannot be found in dic (), that is, the numm value cannot be found in the defined dic, but the if conditional statement can achieve the same effect as defaultdict ().
Code example:
Dic=dict () for num in range (10): if num not in dic: dic [num] = 0max / when the key value of num does not exist in dic, add num and assign it to zero [num] + = 1print (dic)
Output:
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}
These are all the contents of this article entitled "what are the differences between defaultdict and dict in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.