Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic knowledge points of python dictionary

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the basic knowledge points of the python dictionary". In the daily operation, I believe that many people have doubts about the basic knowledge points of the python dictionary. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the basic knowledge points of the python dictionary?" Next, please follow the editor to study!

A dictionary overview dictionary is a set of mappings. it stores key-value pairs and looks up values by keys, rather than index dictionary definitions that represent a dictionary name by braces {} and key-value pairs = {key 1: value 1, key 2: value 2.} key-value pairs are separated by colons Separated by commas between key-value pairs, you can use the function dict () to generate an empty dictionary to look up the value of the dictionary. Generally, the value of the dictionary is looked up through the key of the dictionary. If the value is used to find the key, the book does not say how to look it up. It is estimated that it is also very troublesome to find the corresponding value by enclosing the key in square brackets. Add the key value to the dictionary and add the dictionary name [key] = value so that you can add a new key value to the dictionary. modifying the value of the key value pair to the dictionary is exactly the same as adding the key value pair to the dictionary. when the key dictionary is given, it sometimes modifies the value of the key value pair to delete the key value pair to the dictionary. use the de statement This is how the del dictionary name [key] is used. Method 2: using the pop (key,default) method, he returns the corresponding value and deletes the key-value pair. The method pop () has two parameters, the first is the key of the dictionary to be deleted, and the second is to return the value of default if it is not in the key dictionary to be deleted. The second parameter can be omitted, so the default value will be empty method 3: use method popitem () He will randomly return a key-value pair in the dictionary as a tuple and delete the key-value pair method 4: using the clear () method, he will delete all key-value pairs into an empty dictionary that can contain list values dictionary name [key: list] list can contain dictionary list name [dictionary 1, element 2.] Dictionary can contain dictionary name [key: dictionary] some other operation functions and methods about the dictionary len () function len (dictionary name) returns the length of the dictionary (number of key value pairs) max () function max (dictionary name) returns the maximum value of the key in the dictionary, provided that the keys in the dictionary can be compared with each other, the return key is not the return value The following min () function is also the min () function min (dictionary name) that returns the minimum value of the keys in the dictionary, provided that the keys in the dictionary can be compared with each other. The dict () function generates an empty dictionary keys () method that returns information about all the keys in the dictionary, but the result is an internal data dict_keys in python that specifically represents the dictionary key. If you want to better use the return result, you can use the list () function to generate the list values () method to return the information of all the values in the dictionary, but the return result is an internal data dict_values in python that specifically represents the dictionary value. If you want to make better use of the return result, you can use the list () function to generate list items () method to return the information of all the key-value pairs in the dictionary. But the return result is an internal data type of python dict_items, if you want to make better use of the returned results, you can use the list () function to generate a list, in which the elements of the list are tuples that represent key-value pairs. This is the end of the study on "what are the basic knowledge points of the python dictionary?" I hope to be able to solve your 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report