Study notes (11): Do you really understand Python entry to actual combat-dictionaries and collections? (two)

Learn now: https://edu.csdn.net/course/play/26676/338781?utm_source=blogtoedu

dict([(key,value),(key,value)])

It is not necessarily established in the way of =.

Find the value by index. If the key is not in the dictionary, the program will report an error.

d.get('b'), if the key of b does not exist in the dictionary, then the return is none, and no error will be reported

d.get('b','beifeng') If there is no b key, it will automatically return the value I specified.

d.setdefault('b') creates a key and returns the value corresponding to the key.

Add key-value pairs in the dictionary: d.update()

Delete key-value pairs in the dictionary: d.pop (key), delete the key, and return the value corresponding to the key.

d.pop('lang','hahaha'), if there is no such key, no error will be reported and hahaha will be returned.

d.popitem(), no parameters can be passed, the default is to delete from the last key-value pair. And return the deleted key-value pair. The key-value pairs of the dictionary after python3.6 version are arranged in the order in which the dictionary was created.

Dictionaries are not sequences, but like sequences, they are both container objects and mutable objects. (Element changes will not cause the dictionary object to change) But the dictionary is also ordered.

 

 

 

Guess you like

Origin blog.csdn.net/m0_49939521/article/details/108573632