1. deep copy and shallow copy
# ### deep and shallow copy copy "" " A. 7 = B = A A =. 8 Print (B) LST1 = [l, 2,3] lst2 = LST1 lst1.append (. 4) Print (lst2) Day " " " # shallow copy of a copy of the same name modules in this method is also called copy # method 1 (recommended) " "" Import copy LST1 = [l, 2,3] lst2 = copy.copy (LST1) lst1.append (. 4) Print ( lst2) "" " # method two # listing .copy LST1 = [l, 2,3 ] lst2 = lst1.copy () lst1.append ( . 5 ) Print (LST1) Print (lst2) #Deep copy copy module has a method called DeepCopy "" " # shallow copy can copy all the elements in the list of one, two or more stages of elements can not be copied, so the lead deep copy lst1 = [1,2,3 , [4,5,6,7]] lst2 = lst1.copy () LST1 [-1] .append (. 7) Print (lst2) "" " Import Copy LST1 = [l, 2,3, [4,5 ,. 6 ]] lst2 = copy.deepcopy (LST1) LST1 [ -1] .append (. 9 ) Print (lst2) # deep copy dictionary DIC1 = { " A " :. 1, " B " : [l, 2,3 ] } DIC2 = copy.deepcopy (DIC1) DIC1 [ " B " ].insert(2,4) Print (DIC2) Print (DIC1) # deep copy is larger than the space occupied by shallow copy, slower speed; # multistage deep copy container, a container with a shallow copy
2. dictionaries correlation function
# ### 字典相关函数 dic = {} # "top":"the shy","middle":"faker","bottom":"uzi","support":"rookie","jungle":"厂长" # 增 dic["top"] = "the shy" dic["middle"] = "faker" dic["bottom"] = "uzi" dic["support"] = "mata" dic["jungle"] = "kakao" Print (DIC) # fromkeys () using default values and a set of keys to create the dictionary (the initial value assigned to the current key is not recommended) LST = [ " Top " , " Middle " , " bottom " ] DIC = {} .fromkeys ( LST, None) Print (DIC) # attention point (list) LST = [ " Top " , " Middle " , " bottom " ] DIC = {} .fromkeys (LST, []) Print (DIC) DIC [ " Top " ] .append (. 1 ) Print (DIC) # puncturing DIC = { ' Top ' : ' The Shy ' , ' Middle ' : ' Simon ' , ' bottom ' : ' Uzi ' , ' Support ' : ' Mata ' , ' Jungle ' : ' Kakao ' } # POP () to delete the key to (if not the key to set the default value, error prevention) through key # RES = dic.pop ( "Middle") # Print (say) # Print (RES) MyKey = " jungle123 " RES = dic.pop (MyKey, " the key does not exist " ) Print (RES, dic) # popitem () to delete the last key-value pair RES = dic.popitem () Print ( RES) Print (dic) # the Clear () clear the dictionary dic.clear () Print (dic) # change # update () batch updates (there is the key to update, not the key is added) dic = { " LDB " : " Lu Tung-pin " , " LCH " : " blue and" , " TGL " : " Intuit deviation " , " ZGL " : " Zhang fruit old " , " xboyww " : " mysterious boy " } dicnew = { " HXG " : " Xiangu " , " xboyww " : " Wang " } dic.update (dicnew) Print (DIC) # check # GET () acquired by the key value (if the key can not set the default value,Error prevention) DIC = { "LDB " : " Lu Tung-pin " , " LCH " : " blue and " , " TGL " : " Intuit deviation " , " ZGL " : " Zhang fruit old " , " xboyww " : " mysterious boy " } # Print (dic [ "xboyww1111111"]) # normal if acquired, when the key is not given a direct Print (dic.get ( " xboyww1111111 " , "The key does not exist " )) #methods can get when this key does not exist, set a default value, no error DIC = { " LDB " : " Lu Dongbin " , " LCH " : " blue and " , " TGL " : " te deviation " , " ZGL " : " Zhang fruit old " , " xboyww " : " mysterious boy " } # keys () will form a new dictionary of key iterable RES = dic. Keys () Print (RES) # values () values to form a new dictionary iterables res =dic.values () Print (RES) # items () make up the keys of the dictionary for a tuple to form a new iteration may be subject RES = dic.items () Print (RES)
Day07