dict is a common data structure in the python, which master should try to use
""" Initializing a dict of four ways: 1. dict () -> Create an empty dict 2. dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs 3. dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v 4. dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) """ # The first way D = dict () # 第二种方式 # d = {"person": {"name": "admin", "age": 12}, # "animal": {"name": "gaodan", "age": 3}, # } # Third mode D = dict ({ " name " : " ADMIN " }) Print (D) # Fourth embodiment D = dict (name = ' ADMIN ' , Age = 12 is ) print(type(d)) print("原字典:%s" % d) # {'name': 'admin', 'age': 12} # Clear, emptied # d.clear () # Print (D) # Copy shallow copy new_d = d.copy () Print ( " Copy the new dictionary:% S " % new_d) # { 'name': 'ADMIN', 'Age':} 12 is new_d [ ' name ' ] = ' the root ' Print ( ' Copy dictionary after revision:% S ' % new_d) # Copy dictionary after revision: { 'name': 'the root', 'Age': 12 is} Print ( " original dictionary after modification:% S " % D) # { 'name': 'ADMIN', 'Age':} 12 is # summary: copy shallow copy simple data structures are created when a new object, modify the value of the new dict, without causing the original value of dict Variety print('*' * 50) d = {"person": {"name": "admin", "age": 12}, "animal": {"name": "gaodan", "age": 3} } print(d) # {'person': {'name': 'admin', 'age': 12}, 'animal': {'name': 'gaodan', 'age': 3}} new_d = d.copy() new_d['person']['name'] = 'root' print(new_d) # {'person': {'name': 'root', 'age': 12}, 'animal': {'name': 'gaodan', 'age': 3}} print(d) # {'person': {'name': 'root', 'age': 12}, 'animal': {'name': 'gaodan', 'age': 3}} # Summary: shallow copy: copy a shallow copy complex data structures, the only point of this reference complex data structures, and do not create a new object so modified when new_d the value of the new dict, the value of original dict is also changed. If it is a simple data structure, the absence of such, the above examples also """ Deep copy of the required import copy python copy.deepcopy() """ print('--------------------deep copy------------------') import copy d = {"person": {"name": "admin", "age": 12}, "animal": {"name": "gaodan", "age": 3} } print(d) dd = copy.deepcopy(d) print(dd) # {'person': {'name': 'admin', 'age': 12}, 'animal': {'name': 'gaodan', 'age': 3}} dd['person']['name'] = '哑巴' print(dd) # {'person': {'name': '哑巴', 'age': 12}, 'animal': {'name': 'gaodan', 'age': 3}} print(d) # {'person': {'name': 'admin', 'age': 12}, 'animal': {'name': 'gaodan', 'age': 3}} # Fromkeys, create a new dict, key values in the sequence D = dict.fromkeys ([ ' Jet ' , ' Lily ' ], { " name " : " MAM " }) Print (D) # { 'Jet' : { 'name': 'mam '}, 'lily': { 'name': 'mam'}} # . GET values, is not None value = d.get ( " Jet " ) Print (value) # { 'name': 'MAM'} D = { " name " : ' ADMIN ' , ' Age ' : 12 is } # Items方法 k = d.items () for k in and k: print (k, v) keys = d.keys() print(keys) # dict_keys(['name', 'age']) # POP (key) corresponding to the specified key is removed kV # value = d.pop ( 'name') # key to delete the name # Print (value) ADMIN # # Print (D) {# 'Age':} 12 is # Popitem removed, followed by one Item popitem = d.popitem () Print (popitem) # ( 'Age', 12 is) removed the mapping relationship Print (D) # { 'name': 'ADMIN'} d left this stuff d = {"name":'admin','age':12} # SetDefault (k, v), if there are in the original dict k, returns to the original dict k -> v, otherwise it will k, v dict added to the default_value = d.setdefault ( ' FEMALE ' , ' Ali ' ) Print (default_value) Print (D) # { 'name': 'ADMIN', 'Age': 12 is, 'FEMALE': 'Ali'} default_value = d.setdefault ( ' FEMALE ' , ' JD ' ) Print (default_value) # Ali Print (D) # { 'name': 'ADMIN', 'Age': 12 is, 'FEMALE': 'Ali'} # Update to add elements dict. # The first method uses the update, the attention key is not quoted # d.update (Book = 'Python', = Teacher 'Dog') Print (D) # { 'name': ' admin ',' age ': 12 ,' female ':' ali ',' book ':' python ',' teacher ':' dog '} # Using the second embodiment of the update d.update ([( ' Teacher ' , ' Dog ' ), { ' Book ' , ' Java ' }]) # Good like hanging Print (D) # { 'name': ' admin ',' age ': 12 ,' female ':' ali ',' teacher ':' dog ',' book ':' java '} # values vs = d.values() print(vs) #dict_values(['admin', 12, 'ali', 'dog', 'java'])