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/02 Report--
This article introduces the knowledge of "how to use the basic dictionary of Python programming". Many people will encounter such a dilemma in the operation of actual cases, 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!
I. the concept of dictionary
Dictionaries (dict) are probably the most important Python built-in data structures, and the more common names are HashMap-Java or Associate Array-PHP. It is a collection of key-value pairs (Key-Value Pair), and both keys and values are Python objects. The delimiter is the curly braces {}, and the key and the value are separated by a semicolon.
In fact, Python's dictionary is very similar to JSON.
JSON refers to JavaScript object representation (JavaScript Object Notation)
JSON is a lightweight text data interchange format, and JSON is language independent: JSON uses Javascript syntax to describe data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries support many different programming languages. At present, many dynamic (PHP,JSP,.NET) programming languages support JSON. JSON is self-descriptive and easier to understand.
Use Json Viewer tools to format JSON objects or JSON arrays
Format JSON
Switch to the Viewer tab to visualize the JSON
Dictionary operation (1) create a dictionary 1, create an empty dictionary first, and then add elements (key-value pairs)
When assigning a value to a dictionary key, if the key does not exist, a new element is added, otherwise the old value is replaced with the new value
2. Directly create a dictionary containing several key-value pairs
(2) Dictionary operation 1. Read dictionary elements-key values
Key value, if the key exists, then return value; if the key does not exist, then throw an exception (KeyError)
2. Add field elements-keystroke assignment
Key assignment, add new elements (keys that do not exist)
If the key exists when the key is assigned, it is not adding a new element, but updating the value of the old element
Only new elements are allowed to be added, not old ones.
You can split a dictionary into two lists: a key list and a values list
3. Update dictionary elements
(1) Direct keystroke assignment (the key must exist)
When a key is assigned, if the key exists, the dictionary element is updated, otherwise a new element is added.
Only elements are allowed to be updated, not new elements are allowed.
(2) you can use the update () method to insert or update to view the parameters of the update () function.
Update the names dictionary according to the dictionary {'003: Wa', '006: Transformers'}
4. Traversing dictionary elements
Use the keys () function of the dictionary to get the list of keys, and then use the for loop to traverse
5. Delete dictionary elements
(1) use the del command to delete the dictionary element with the key "003" (success) and delete the dictionary element with the key "007" (exception)
(2) use the pop () function of the dictionary to delete dictionary elements and view the parameters and return values of the pop () function.
Delete dictionary element with key "005" (success), delete dictionary element with key "008" (exception)
6. Split the dictionary into a key list and a values list
Use the keys () and values () methods to return a key list and a values list (iterator), and the two lists are in the same order
7. Convert the dictionary into a list of key-value tuples
8. Create a dictionary based on two equal-length lists
Create dictionaries based on city list and city flower list
There's a simpler way.
9. Default values for dictionary element acquisition
Note that by getting the dictionary element through the get () function, you can set the default value, which defaults to None. Press the key to take the value, and if the key exists, the corresponding value is returned, otherwise the default value is returned.
Homework task 1. Classify the word list into a list dictionary according to the first letter.
Method one
Method two
Task 2. Count the number of words in the file.
This is the end of the content of "how to use the basic dictionary of Python programming". Thank you for 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.