Python dictionary operations
# Dictionary type# Create a dictionary # empty dictionary var = dict () print (var, type (var) var ={} print (var, type (var )) # create a dictionary with multiple data # 1.var = {'black': 'black', 'white': 'white', 'blue': 'blue'} print (var, type (var) # 2.var = dict ({'black': 'black', 'white': 'white', 'blue': 'blue'}) print (var, type (var) # 3.var = dict (black = 'black', white = 'white', blue = 'blue') print (var, type (var )) # 4.var = [['black', 'black'], ['white', 'white'], ['blue', 'blue'] result = dict (var) print (result, type (result) #5. keys = ['black', 'white', 'lan'] values = ['black', 'white', 'blue'] result = dict (zip (keys, values) print (result, type (result) # element-> key-Value Pair-> key + value # basic operation var = {'zhao yun': 'zi long ', 'pontong ': 'shi yuan', 'Guo jia': 'fengxiao', 'lu Su': 'zi Jing '} # access dictionary: print (var ['zhao yun']) # modifying the dictionary var ['Guo jia'] = 'tian wei' print (var) # deleting the element del var ['Guo jia'] print (var) # add element var ['zhou Yu '] = 'gong Jin' print (var) # sequence operation # member detection-> key-specific operation instead of value var = {'zhao yun ': 'zilong ', 'pontong': 'shi yuan ', 'Guo jia': 'feng Xiao', 'lu Su ': 'zi Jing '} result = 'shi yuan' in varprint (result) # len () checks the number of elements in the dictionary var = {'zhao yun': 'zi long', 'pontong ': 'shi yuan', 'Guo jia': 'feng Xiao ', 'lu Su': 'zi Jing'} result = len (var) print (result) # max () obtain the largest key in the dictionary. If it is not a numeric key, obtain the maximum encoded value var = {'A': 1, 'B': 2, 'C': 3, 'blood ': 12} result = max (var) print (result) # min () obtain the smallest key in the dictionary. If it is not a numeric key, obtain the smallest encoded value var = {'A': 1, 'B': 2, 'C': 3, 'clerk': 12} result = min (var) print (result) # dictionary traversal var = {'zhao yun ': 'Child long', 'pontong ': 'shi yuan', 'Guo jia': 'feng Xiao ', 'lu Su': 'zi Jing'} # Method 1: traverse the key first, then use the variables and keys to obtain the corresponding value for I in var: print (I, var [I]) # Method 2: simultaneously traverse the key and value result = var. items () print (result, type (result) # It is not the standard list type for k, v in var. items (): print (k, v) # dictionary derivation var = {'ag': 'sfsf ', 'bg': 'fsdf ', 'cg ': 'df'} # General derivation result = {k: v for k, v in var. items ()} print (result) # derivation result with judgment conditions = {k: v for k, v in var. items () if len (v) = 2} print (result) # multi-cycle dictionary derivation girls = {'zb': 'bo', 'mm ': 'mei ', 'JJ': 'jun'} boys = {'ll ': 'le', 'ff': 'fei', 'ji ': 'jiao'} result = {g + B: gv + bv for g, gv in girls. items () for B, bv in boys. items ()} print (result) # dictionary-related function var = {'sunwalker ': 'sun Wukong', 'sha monk ': 'sha Wujing', 'pig Ba Jie ': 'piuwene'} # clear the dictionary var. clear () print (var, id (var) # copy () copy the dictionary newvar = var. copy () print (newvar, id (newvar) # fromkeys () uses the sequence (key) and specified value (value) to create a dictionary list = ['fda ', 'fsd', 'jj', 'hfg'] result = dict. fromkeys (list, 'birds') result = {}. fromkeys (list, 'birds') print (result) # get () get the value of the specified key in the dictionary result = var. get ('pig', 'default') # result = var ['pig'] print (result) # setdefault () add an element to the dictionary [a key that does not exist is added, the existing key does not perform any operation] var. setdefault ('little white long', 'Little white long') print (var) # update () modifies the element var in the dictionary. update (Sun Walker = 'Sun monkey ') print (var) var. update ({'sunwalker ': 'qitian dasheng', 'pig bag': 'tianpeng Marshal ', 'tang san': 'tang xuanjicang'}) print (var) # pop () delete the specified Element result = var in the dictionary. pop ('tangsan') print (var) print (result) # popitem () randomly deletes an element in the dictionary. result = var. popitem () print (var) print (result) # keys () gets the container var = {'apple': 'apple', 'pe' consisting of all keys in the dictionary ': 'pear '} result = var. keys () print (result) for I in result: print (I) # values () gets the result of the container consisting of so values in the dictionary = var. values () print (result) # items () obtains the Level 2 Container result = var composed of all keys and values in the dictionary. items () print (result)