python dictionary dict learning summary

1. The representation of the dictionary : expand it with curly brackets, and connect the elements between the key-value pairs with colons (key:value)

E.g:
di = {'a1': '123', 'b1': '456', 'c1': '789'}
Here, although there are 6 strings in di, in fact, di has only 3 values. We call the value in di of 'a1':'123' as 'key-value pair' (key:value). Among them, value The part can use any value, and can be nested at will; and in the key part, the list list and the dictionary dict cannot be placed in the key, which is related to the operation of the dictionary itself (because the list dictionary cannot do hash operations, it cannot be used as a key value) The last thing to note is that the dictionary is unordered ; the advantage of being unordered is that when storing a lot of things, it can be read quickly, which saves time
 



2. Addition, deletion, modification and search of dictionary and other methods

a. to increase

.fromkeys() (iterable sequence, value) Static method; according to an iterable sequence, use it as a key to create a dictionary, and assign a uniform value values

b. delete

del['a1'] Delete key-value pairs by index (the index of the dictionary is key)
.clear() deletes all elements in the specified dictionary
.pop() (specified key, return value) Delete the specified key in the dictionary; when the specified key exists, the corresponding value is returned after deletion; if it does not exist, the specified return value is returned; if the return value is not set and the specified key does not exist, an error will be reported
.popitem () Delete elements from a dictionary in python's order (randomly), one at a time, returning the deleted key-value pair
.setdefault() (specified key, specified value) Set the specified key-value pair, if the specified key already exists, return the corresponding value, if not, add it to the dict and return its value

c. Modify


.update() (any key-value pair) #Multiple pairs are separated by commas Update the key-value pair in the dict, update if there is one, add if not
#Here key-value pairs can be separated by colons such as 'a1':'123' or separated by equal signs, so I won't do more examples

d. Inquiry

The query method of dictionary dict is different from that of list list and tuple tuple. Both list list and tuple tuple can be queried through simple numerical indexes such as lst[0] and slice; but the dictionary is different. The index of the dictionary is defined by us. key, such as print(di['a1']); and because the dictionary dict is unordered, it is impossible to view the dictionary by slicing

For the dictionary, because the key is customized when it is created, the while method is invalid for the dictionary, but the elements in the dictionary can be output through the for loop

.keys() Output the key in the specified dictionary, the return value type is 'dict_keys', and the official description is 'a set-like object'
.values() Output the values ​​in the specified dictionary, the return value type is 'dict_values'
.items() Output the key-value pairs in the specified dictionary, the return value type is 'dict_items'
.get() (specified key, return value) Query whether there is a specified key in the dictionary, if there is, output the corresponding value, if not, output the specified return value, the return value defaults to None
The difference between #get() and index lookup is that index lookup will report an error when it is not found and get() method will not report an error

e. Other

.copy() Copy the specified dictionary (shallow copy)
浅复制是什么呢?就是,本来有字典 a = {'abc':'123','efg':'456'},在内存中,就是把a这个变量指向 {'abc':'123','efg':'456'}在内存中的地址,当我们做一个浅复制 b = a.copy()   时,并不是说 {'abc':'123','efg':'456'}做了一个新的副本,而是把b这个变量也指向了 {'abc':'123','efg':'456'}, 也就是说a和b指向的是同一个内存地址 ,这种复制叫浅复制

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325777548&siteId=291194637