In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the operations of python dictionaries". In the daily operation, I believe many people have doubts about the operation of python dictionaries. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the operations of python dictionaries?" Next, please follow the editor to study!
Does key exist?
Use the in keyword to determine whether key exists in the dictionary
D = {"name": "zhang", "age": 10}
Key = "gender"
If key in d:
Pass
Do not use key in d.keys (), which is a superfluous operation, because d.keys () returns a new list object, resulting in maximum memory.
Merge dictionary > x = {'averse: 1,' baked: 2}
> y = {'baked: 3,' cached: 4}
> > {* x, * y}
{'averse: 1,' baked: 3, 'catered: 4}
Return the new dictionary object, x and y will not change, this is using the PEP448 normalized unpacking operation, if x and y have the same key, then y will overwrite x. If you use the dictionary's update method, the original object is modified, for example:
> > y.update (x)
> > y
{'baked: 2,' cached: 4, 'averse: 1}
Iterative dictionary z = {'baked: 2,' cached: 4, 'averse: 1}
> > for key, value in z.items ():
Print (key, value)
B 2
C 4
A 1
You can also iterate over the dictionary z directly, and each iteration returns key, and the previous operation z.items () returns a new object.
> for key in z:
Print (key, z [key])
B 2
C 4
A 1
Dictionary derivation
Using dictionary inference to quickly build dictionary objects
> fruits = ['apple',' mango', 'banana','cherry']
> > {f:len (f) for f in fruits}
{'apple': 5,' mango': 5, 'banana': 6,' cherry': 6}
Dictionary sort
A dictionary is essentially an unordered container object (in fact, Python3.6 begins to support ordering, but this order refers to the order in which key is added). If we want to sort the dictionary's key or value, we usually convert it to list and sort it by key or value.
> d = {"c": 3, "a": 1, "f": 6, "b": 0}
# sort by value
> sorted (d.items (), key=operator.itemgetter (1))
[('baked, 0), (' axed, 1), ('crested, 3), (' favored, 6)]
# sort by key
> sorted (d.items (), key=operator.itemgetter (0))
[('axiang, 1), (' baked, 0), ('cased, 3), (' favored, 6)]
> > >
Default initialization dictionary
Count the number of times each letter appears in the list
> chars = ['axiaqu', 'bang,',',]
> data = {}
> for c in chars:
... If c in data:
... Data [c] + = 1
... Else:
... Data [c] = 0
...
> data
{'asides: 1,' breadth: 0, 'cations: 1,' dudes: 0}
You can use collections.defaultdict to provide initialization defaults through the factory function
Chars = ['axiajiaoyao' bangzhongjiaozhuangzhuangzhuangzhuangzhuangyuanzhangyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyangzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuo []
Dd = defaultdict (int)
For c in chars:
Dd [c] + = 1
> defaultdict (, {'averse: 2,' baked: 1, 'cached: 2,' dumped: 1})
Defaultdict is a subclass of dict and inherits all the features of dict. What if you want the initial value to be 1? You can use the lambda function to specify
Dd = defaultdict (lambda: 1)
For c in chars:
Dd [c] + = 1
Print (dd)
> defaultdict (, {'averse: 3,' baked: 2, 'cached: 3,' dashed: 2}) this ends the study of "what is the operation of python dictionaries". I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.