I. supplement previous knowledge points
1. join method in str. convert list to string
# Convert list to string,Between each element_Splicing s = "_".join(["day","land","people"]) print(s) ss = "day_land_people" s = ss.split("_") print(s)
2. The list and dictionary cannot be deleted directly during the cycle
The deleted content needs to be recorded in the new list
Then loop through the new list and delete the dictionary or list
lst = ["a","b","c"] n = [] for a in lst: n.append(a) for b in n: lst.remove(b) print(lst)
3.fromkeys()
1. Return to the new dictionary.. no effect on the original dictionary
2. The next value is that multiple key s share a value
dic = {"apple":"Apple","banana":"Banana"} ##Return to the new dictionary, it has nothing to do with the original one ret = dic.fromkeys("orange","A mandarin orange") #Direct access with dictionary fromkeys No impact on dictionary print(ret) ret = dic.fromkeys("ab",["Ha-ha","Ha-ha","Roar"]) #fromkeys Access directly with class name print(ret)
Set set
Features: out of order, no repetition, elements must be hashable (immutable)
Function: de duplication
It is a variable data type, with addition, deletion, modification and query operations
Add:
s = {"Blademaster","Sword hero","Sword devil"} s.add("Jian Ji") print(s) #{'Jian Ji', 'Sword devil', 'Sword hero', 'Blademaster'} s.add("Blademaster") print(s) #Not to be added repeatedly {'Jian Ji', 'Sword devil', 'Sword hero', 'Blademaster'} s.update("Heaven rewards men") #Iterative addition print(s) #{'day', 'Sword hero', 'Sword devil', 'Avenue', 'Reward', 'Diligence', 'Blademaster'} Disordered
Delete:
s = {"Hunting","Genji","Semi reservoir","death"} ret = s.pop() #Randomly delete an element print(ret) s.remove("Genji") #Delete elements directly print(s) #{'death', 'Hunting', 'Semi reservoir'} s.clear() #empty set aggregate.It should be noted that set If the set is empty. Printed out yes set() Because we want to dict Distinguishable. print(s)
Change:
# set The data in the collection does not have an index. There's no way to locate elements. So there's no way to modify it directly. # We can use delete first and then add to complete the modification s = {"Hunting","Genji","Semi reservoir","death"} s.remove("Hunting") print(s) #{'death', 'Genji', 'Semi reservoir'} s.add("Fortress") print(s) #{'Genji', 'Semi reservoir', 'death', 'Fortress'}
Check:
# set Set is an iterative object,available for Loop to find s = {"Hunting","Genji","Semi reservoir","death"} for e in s: print(e)
Frozen collection. Immutable. Hashable
s = frozenset(["Zhao Benshan","Often expensive","Leather mountain"]) print(s) #frozenset({'Zhao Benshan', 'Often expensive', 'Leather mountain'}) dic = {s:"123"} #{frozenset({'Zhao Benshan', 'Often expensive', 'Leather mountain'}): '123'} print(dic)
Other operations of the collection:
s1 = {"Lennon","Zhao Si","Leather mountain"} s2 = {"Chief Liu","Feng Xiang Long","Leather mountain"} # intersection # Common elements in two sets print(s1 & s2) #{"Leather mountain"} print(s1.intersection(s2)) #{"Leather mountain"} # Union print(s1|s2) #{'Feng Xiang Long', 'Chief Liu', 'Leather mountain', 'Lennon', 'Zhao Si'} print(s1.union(s2)) #{'Feng Xiang Long', 'Leather mountain', 'Lennon', 'Chief Liu', 'Zhao Si'} # Difference set print(s1 - s2) # {'Zhao Si', 'Lennon'} Get the independent print(s1.difference(s2)) #{'Lennon', 'Zhao Si'} # Inverse intersection print(s1^s2) #Separate data in two sets {'Chief Liu', 'Lennon', 'Feng Xiang Long', 'Zhao Si'} print(s1.symmetric_difference(s2)) #{'Chief Liu', 'Lennon', 'Feng Xiang Long', 'Zhao Si'} s1 = {"Lennon","Zhao Si"} s2 = {"Lennon","Zhao Si","Leather mountain"} # subset print(s1<s2) #set1 yes set2 Subsets?? Ture print(s1.issubset(s2)) # Superset print(s1>s2) #set1 yes set2 Supersets?? False print(s1.issuperset(s2)) s = frozenset(["Zhao Benshan","Lennon","Leather mountain","Often expensive"]) dic = {s:"123"} print(dic) #{frozenset({'Often expensive', 'Leather mountain', 'Lennon', 'Zhao Benshan'}): '123'}
III. deep and shallow copy (difficulty)
1. Assign a value. No new object is created. The same object is shared
lst1 = ["Golden King","Purple shirt Dragon King","King of white eagle","Green Wing bat King"] lst2 = lst1 print(lst1) #['Golden King', 'Purple shirt Dragon King', 'King of white eagle', 'Green Wing bat King'] print(lst2) #['Golden King', 'Purple shirt Dragon King', 'King of white eagle', 'Green Wing bat King'] lst1.append("Yang Xiao") print(lst1) #['Golden King', 'Purple shirt Dragon King', 'King of white eagle', 'Green Wing bat King', 'Yang Xiao'] print(lst2) #['Golden King', 'Purple shirt Dragon King', 'King of white eagle', 'Green Wing bat King', 'Yang Xiao'] dic1 = {"id":123,"name":"Thankson"} dic2 = dic1 print(dic1) #{'id': 123, 'name': 'Thankson'} print(dic2) #{'id': 123, 'name': 'Thankson'} dic1["name"] = "Fan Yao" print(dic1) #{'id': 123, 'name': 'Fan Yao'} print(dic2) #{'id': 123, 'name': 'Fan Yao'}
2. Shallow copy. Copy the first level content. [:] or copy()
#Shallow copy lst1 = ["He Jiong","Du Haitao","Zhou Yumin"] lst2 = lst1.copy() lst1.append("Li Jiacheng") print(lst1) print(lst2) print(id(lst1),id(lst2)) #Result # ['He Jiong', 'Du Haitao', 'Zhou Yumin', 'Li Jiacheng'] # ['He Jiong', 'Du Haitao', 'Zhou Yumin'] # 1653894977480 1653895005704 # Two lst Quite different,Memory address and content are not the same,Found that a copy of memory was implemented lst1 = ["He Jiong","Du Haitao","Zhou Yumin",["Gentiana","Ma Yun","Zhou Bichang"]] lst2 = lst1.copy() lst1[3].append("How lonely is invincible") print(lst1) print(lst2) print(id(lst1[3]),id(lst2[3])) # Result # ['He Jiong', 'Du Haitao', 'Zhou Yumin', ['Gentiana', 'Ma Yun', 'Zhou Bichang', 'How lonely is invincible']] # ['He Jiong', 'Du Haitao', 'Zhou Yumin', ['Gentiana', 'Ma Yun', 'Zhou Bichang', 'How lonely is invincible']] # 1479533238216 1479533238216 # Shallow copy:Only the first layer will be copied,The content of the second layer will not be copied,So it's called a shallow copy
3. Deep copy. Copy all contents, including all internal
#Deep copy import copy lst1 = ["He Jiong","Du Haitao","Zhou Yumin",["Gentiana","Ma Yun","Zhou Bichang"]] lst2 = copy.deepcopy(lst1) lst1[3].append("How lonely is invincible") print(lst1) print(lst2) print(id(lst1[3]),id(lst2[3])) #Result # ['He Jiong', 'Du Haitao', 'Zhou Yumin', ['Gentiana', 'Ma Yun', 'Zhou Bichang', 'How lonely is invincible']] # ['He Jiong', 'Du Haitao', 'Zhou Yumin', ['Gentiana', 'Ma Yun', 'Zhou Bichang']] # 1589080305224 1589080421384