In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The purpose of this article is to share with you about the examples of routine operation and introduction of dictionaries in Python. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
1. An introduction to the dictionary
Dictionaries are another mutable type and can store objects of any type.
Each key value key= > value of the dictionary is divided by a colon: split, each pair is separated by a comma (,), and the entire dictionary is included in curly braces {} in the following format:
Dic = {key1: value1, key2: value2, key3: value3}
The key must be unique, but the value is not necessary.
Values can take any data type, but keys must be immutable, such as strings, numbers.
A simple dictionary example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} 2. The value of access dictionary (1) access the value according to the key
When the key of the dictionary is stored, you can get the corresponding value.
For example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic ['name']) print (dic [' age']) print (dic ['sex'])
An error will be reported when the key does not exist
For example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic ['score'])
(2) access the value through the get () method
You can get the corresponding value when the key exists.
For example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic.get ('name'))
When the key does not exist, None is returned
For example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic.get ('a'))
3. Modify the value of the dictionary
The data in each element of the dictionary can be modified as long as it is found through key.
For example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} dic ['name'] =' python'print (dic)
4. Add elements of the dictionary (key-value pairs)
When you add elements in a dictionary, you add them in the form of key-value pairs
If the "key" is in the dictionary when using the variable name ['key'] = data, the data is modified and does not exist, then this element will be added.
For example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} dic ['address'] =' Beijing 'print (dic)
5. Delete elements of a dictionary
You can delete a single element and empty the dictionary.
Delete the specified element, for example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} del dic ['name'] print (dic)
Empty the entire dictionary, for example:
Dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} dic.clear () print (dic)
6. Dictionary common operations the number of key-value pairs in the 1.len measurement dictionary dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (len (dic))
2. Keys returns a list of all the KEY of the dictionary dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic.keys ())
3. Values returns a list of all the value of the dictionary dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic.values ())
4. Items returns a list of all meta-ancestors dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} print (dic.items ())
5. Key (key) dic of the ergodic dictionary = {'name':' xiaohua', 'age': 18,' sex': 'male'} for k in dic.keys (): print (k)
6. Value (value) dic of the ergodic dictionary = {'name':' xiaohua', 'age': 18,' sex': 'male'} for v in dic.values (): print (v)
7. Items (element) dic of the ergodic dictionary = {'name':' xiaohua', 'age': 18,' sex': 'male'} for i in dic.items (): print (I)
8. The items (key-value pair) dic of the ergodic dictionary dic = {'name':' xiaohua', 'age': 18,' sex': 'male'} for kpene v in dic.items (): print (kmemv)
9. Implement traversal with subscript index
The enumerate () function is used to combine a traverable data object (such as a list, tuple, or string) into an index sequence, listing both the data and the data subscript, which is commonly used in the for loop.
Chars= (chars): print (iMagna in enumerate)
These are the general examples and introductions of dictionaries in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.