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/01 Report--
This article introduces the knowledge of "what are the common methods of python dictionaries". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. The concept of dictionary
Like a list, a dictionary is also a variable sequence, but unlike a list, it is an unordered variable sequence. The contents saved are stored in the form of key-value pairs (key:value). Each key-value of the dictionary is separated by a colon: separated, separated by each key-value pair, and the whole dictionary is contained in {}.
Dict = {key1:value1,key2:value2} 2. The main features of dictionaries
1: read by key rather than by index
2: a dictionary is an unordered collection of arbitrary objects
3: dictionaries are variable and can be nested at will
4: dictionary keys must be unique
5: the keys of the dictionary must be immutable
3. Three ways to create a dictionary # the first method dic1 = {'name':'hacker','age':'18'} # the second method dic2 = dict (name='hacker',age='18') # the third method dic3 = dict ([(' name','hacker'), ('age','18')]) 4. The common method of dictionary 1.clear ()
Define the clear () method to clear all elements in the dictionary (return empty dictionary). Give me a chestnut. Clear all elements in the car dictionary
Car = {"brand": "Porsche", "model": "911", "year": 1963} car.clear () print (car)
The running results are as follows:
{}
2.copy ()
Define the copy () method to return a copy of the dictionary (copy dictionary). Give me a chestnut. Copy car dictionary
Car = {"brand": "Porsche", "model": "911", "year": 1963} res = car.copy () print (res)
The running result is as follows
{'brand':' Porsche', 'model':' 911, 'year': 1963}
3.get ()
Define the get () method that returns the value of the specified key. Give me a chestnut. Use the get method to return the value of "model"
Car = {"brand": "Porsche", "model": "911", "year": 1963} x = car.get ("model") print (x)
The running results are as follows:
nine hundred and eleven
4.keys ()
Define all keys that return to the dictionary. Give me a chestnut. Returns all keys of the car dictionary
Car = {"brand": "Porsche", "model": "911", "year": 1963} res = car.keys () print (res)
The running results are as follows:
Dict_keys (['brand',' model', 'year'])
5.values ()
Define all the values that return the dictionary. Give me a chestnut. Returns all values of the car dictionary
Car = {"brand": "Porsche", "model": "911", "year": 1963} res = car.values () print (res)
The running results are as follows:
Dict_values (['Porsche',' 911, 1963]) 6.items ()
Define all key-value pairs that return to the dictionary. Give me a chestnut. Returns all key-value pairs of car dictionaries
Car = {"brand": "Porsche", "model": "911", "year": 1963} res = car.items () print (res)
The running results are as follows:
Dict_items ([('brand',' Porsche'), ('model',' 911'), ('year', 1963)])
7.del ()
Define delete dictionary elements? Give me a chestnut. Delete the key "model" of the car dictionary
Car = {"brand": "Porsche", "model": "911", "year": 1963} del car ["model"] print (car)
The running results are as follows:
{'brand':' Porsche', 'year': 1963}
8.zip ()
Define the zip () method to package the key values into a dictionary
Li1 = ["name", "age"] li2 = ["hacker", "18"] print (dict (zip (li1,li2)
The running results are as follows:
{'name':' hacker', 'age':' 18'}
This is the end of the content of "what are the common methods of python dictionaries". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.