In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the dictionaries of Python". Interested friends may wish to take a look at them. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the dictionaries of Python"?
Catalogue
What is a dictionary?
Create a dictionary
1. Direct assignment
two。 Create an empty dictionary using the dict () function
3. Get dictionary elements
4. Unique key. If there are two or more identical keys in the dictionary, the last key is taken by default.
Summary:
An increase in dictionaries
The revision of the dictionary
The deletion of a dictionary
1. Del method
2. Pop method
3. Popitme method
4. Clear method
Look up the dictionary
Dictionary value
1. Keys method
2. Values method
3. Items method
Dictionary replication
Ergodic dictionary
What is a dictionary?
A dictionary is an unordered collection of data in Python, and it is stored using key-value pairs.
Dict = {'key':'value'} create a dictionary
Direct assignment, using curly braces {}
Create an empty dictionary using the Dict function
1. Direct assignment Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Department': 'Cui Jue' print (Dict) print (type (Dict)) > {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Cha Division':'Lu Zhidao', 'Yin Law Division': Cui Jue'} > 2. Use the dict () function to create an empty dictionary Dict = dict () print (Dict) print (type (Dict)) > > {} > > 3. Get dictionary elements Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Cha Division':'Lu Zhidao', 'Yin Law Division': 'Cui Jue'} print (Dict ['Cha Si']) > Lu Zhidao 4. The only key, if there are two or more identical keys in the dictionary, defaults to the last Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Division': 'Cui Jue', 'punishment Division':'Li Ghost'} print (Dict ['penalty Division') > > Li Ghost Summary:
The dictionary consists of a key (key) and a value (value).
Creation can be done using curly braces or the dict function, with elements separated by commas and key values separated by colons.
Note: keys (key) must be immutable types, such as numbers, strings, and values (value) are arbitrary.
Dictionary elements are obtained using the key (key).
The keys are unique and try not to be the same.
Dictionary added Dict [key] = 'value' Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Division': 'Cui Jue'} Dict ['resident'] = 'painting Menglion' print (Dict) > > {'reward Division': 'Wei Zheng', 'penalty Division': 'Zhong Kui', 'Chacha Division':'Lu Zhidao' 'Yin Jue': 'Cui Chueh', 'resident': 'painting Mengshi'} the description of the dictionary is modified directly. The method is similar to adding update to add a dictionary element to another dictionary, and modify it if the dictionary already has that element Otherwise, add Dict [key] = 'value' Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Cha Division':'Lu Zhidao', 'Yin Division': 'Cui Jue'} # modify the corresponding value if the key already exists in the dictionary, otherwise increase Dict ['penalty Division'] = 'painting Dream Lion' print (Dict) > > {'appreciation Division': 'Wei Zheng' "punishment Division": "painting Dream Lion", "Cha Division": "Lu Zhidao", "Yin Law Division": 'Cui Chueh'}
two。 Use the update side
Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Division': 'Cui Jue'} Dict2 = {'Heavenly Master': 'Zhong Kui', 'punishing evil Division': 'Heavenly Master Zhong Kui'} Dict.update (Dict2) print (Dict) > > {'rewarding Department': 'Wei Zheng', 'punishing evil Division': 'Heavenly Master Zhong Kui' 'Cha Shi':'Lu Zhidao', 'Yin Justice': 'Cui Chueh', 'Heavenly Master': 'Zhong Kui'} Dictionary deletion method describes how del deletes an element Or delete the entire dictionary pop delete an element popitme delete the last element in the dictionary clear empty the dictionary 1. Del method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Cha Division':'Lu Zhidao' 'Yin Division': 'Cui Jue'} # Delete an element according to the key del Dict ['penalty Division'] # Delete the entire dictionary # del Dict print (Dict) > > {'reward Division': 'Wei Zheng', 'investigation Division':'Lu Zhidao', 'Yin Division': 'Cui Jue' 2. Pop method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui' 'Cha Dao':'Lu Zhidao', 'Yin Justice': 'Cui Jue'} Dict.pop ('reward Division') print (Dict) > > {'punish evil Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Justice': 'Cui Jue' 3. Popitme method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Inspection Division':'Lu Road' 'Yin Division': 'Cui Jue'} Dict.popitem () print (Dict) > > {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao'4. Clear method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Cha Division':'Lu Zhidao' 'Yin Jue': 'Cui Jue'} Dict.clear () print (Dict) # because the dictionary has been emptied > {} the method of looking up the dictionary describes in to determine whether the element is in the dictionary. The return value is the opposite of Boolnot in and in
Because this is a general method, I have said it many times before, and I have skipped it now.
Dictionary value method to describe keys
Gets the key in the dictionary and returns an iteration
Values gets the value in the dictionary and returns an iterative items to get the key value of the dictionary Return an iteration 1. Keys method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Division': 'Cui Jue'} Key = Dict.keys () print (Key) # for iterative objects, we can traverse it or convert it into a list for operation > > dict_keys (['reward Division', 'punishment Division') 2. Values method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Division': 'Cui Jue'} Value = Dict.values () print (Value) # for iterative objects, we can traverse it or convert it into a list for operation > > dict_values ([Wei Zheng') 'Zhong Kui','Lu Zhidao', 'Cui Jue') 3. Items method Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Division': 'Cui Jue'} Item = Dict.items () print (Item) # for iterative objects, we can traverse it Or convert it into a list for operation > dict_items ([('reward Division', 'Wei Zheng'), ('punishment Division', 'Zhong Kui'), ('Cha Si','Lu Zhidao'), ('Yin Division', 'Cui Jue']) Dictionary copy
Using the copy function as usual
Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Department': 'Cui Jue'} copy_Dict = Dict.copy () Dict.clear () print (Dict) print (copy_Dict) > > {} > > {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Inspection Division':'Lu Zhidao' 'Yin Jue': 'Cui Chueh'} traversing the dictionary
Traversing key
Traversing value
Traversing item
Traversing key-value
1. Traversing key
Dict = {'reward Department': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Justice Department': 'Cui Jue'} for i in Dict.keys (): print (I) > reward Division punishing Evil Division Cha Division Yin Division
two。 Traversing value
Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'Cha Si':'Lu Zhidao', 'Yin Justice': 'Cui Jue'} for i in Dict.values (): print (I) > > Wei Zheng Zhong Kui Lu Zhidao Cui Jue
3. Traversing item
Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Division': 'Cui Jue'} for i in Dict.items (): print (I) > ('reward Division', 'Wei Zheng') ('punishment Division', 'Zhong Kui') ('Cha Shi','Lu Zhidao') ('Yin Justice', 'Cui Jue')
4. Traversing key and value
Dict = {'reward Division': 'Wei Zheng', 'punishment Division': 'Zhong Kui', 'investigation Division':'Lu Zhidao', 'Yin Law Department': 'Cui Jue'} for key, value in Dict.items (): print (key, value) > appreciation Department Zhong Kui punishment Division Lu Zhidao Yin Division Cui Chueh, I believe you have a better understanding of "what is the dictionary of Python"? You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.