One, set.
A collection is an unordered, non-repeating collection of data in which the elements are hashed (immutable types), but the collection itself is not hashed (so the collection cannot be a key to the dictionary). The following are the two most important points of the collection:
Go to the weight, turn a list into a set, and automatically go heavy.
Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set.
1, creation of the collection.
Set1 = Set ({$, ' Barry '}) Set2 = {, ' Barry '}print (Set1,set2) # {1, 2, ' Barry '} {1, 2, ' Barry '}
2, the increase of the collection.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.add (' King goddess ') print (Set1) #update: Iterate to add set1.update (' A ') print (Set1 ) set1.update (' Teacher ') print (Set1) set1.update ([+]) print (SET1)
3, delete the collection.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.remove (' Alex ') # Delete an element print (Set1) set1.pop () # Random deletion of an element print (Set1) set1.clear () # Empty collection print (Set1) del set1 # Delete collection print (Set1)
4, other operations of the collection:
4.1 intersection. (& or intersection)
Set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}print (Set1 & Set2) # {4, 5}print (Set1.intersection (Set2)) # {4, 5}
4.2 and set. (| or union)
Set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}print (Set1 | set2) # {1, 2, 3, 4, 5, 6, 7}
Print (Set2.union (Set1)) # {1, 2, 3, 4, 5, 6, 7}
4.3 difference set. (-or difference)
Set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}print (Set1-set2) # {1, 2, 3}print (Set1.difference (Set2)) # {1, 2, 3}
4.4 Inverse intersection. (^ or symmetric_difference)
Set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}print (set1 ^ set2) # {1, 2, 3, 6, 7, 8}print (Set1.symmetric_difference (Set2)) # {1, 2, 3, 6, 7, 8}
4.5 Subsets and Hyper-sets
Set1 = {1,2,3}set2 = {1,2,3,4,5,6}print (Set1 < Set2) print (Set1.issubset (Set2)) # These two are the same, both of which indicate that Set1 is a subset of Set2. Print (Set2 > Set1) print (Set2.issuperset (Set1)) # These two are the same, both of which indicate Set2 is a set1 superset.
5,frozenset the immutable collection, making the collection immutable.
s = Frozenset (' Barry ') print (S,type (s)) # Frozenset ({' A ', ' Y ', ' B ', ' R '}) <class ' Frozenset ' >
Two, depth copy
1, first look at the assignment operation.
L1 = [1,2,3,[' Barry ', ' alex ']]l2 = l1l1[0] = 111print (L1) # [111, 2, 3, [' Barry ', ' Alex ']]print (L2) # [111, 2, 3, [ ' Barry ', ' Alex ']]l1[3][0] = ' wusir ' Print (L1) # [111, 2, 3, [' Wusir ', ' Alex ']]print (L2) # [111, 2, 3, [' Wusir ', ' al ' Ex ']]
For an assignment operation, L1 and L2 point to the same memory address, so they are exactly the same.
2, shallow copy copy.
L1 = [1,2,3,[' Barry ', ' Alex ']
L2 = L1.copy () print (L1,id (L1)) # [1, 2, 3, [' Barry ', ' Alex ']] 2380296895816print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex '] 2380296895048
L1[1] = 222
Print (L1,id (L1)) # [1, 222, 3, [' Barry ', ' Alex ']] 2593038941128
Print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2593038941896
L1[3][0] = ' wusir ' Print (L1,id (l1[3])) # [1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016print (L2,id (l2[3])) # [1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016
For shallow copy, the first layer creates a new memory address, and from the second level it points to the same memory address, so consistency is maintained for the second layer and the deeper layers.
3, deep copy deepcopy.
Import COPYL1 = [1,2,3,[' Barry ', ' alex ']]l2 = copy.deepcopy (L1) print (L1,id (L1)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377 167816print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[1] = 222print (L1,id (L1)) # [1, 222, 3, [' Bar Ry ', ' Alex ']] 2915377167816print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[3][0] = ' wusir ' Print (L1, ID (l1[3]) # [1, 222, 3, [' Wusir ', ' Alex ']] 2915377167240print (L2,id (l2[3])) # [1, 2, 3, [' Barry ', ' Alex ']] 291537 7167304
For deep copy, the two are completely independent, changing any element of any one (no matter how many layers), and the other absolutely does not change.
Python Collection Depth Copy