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'}# {1, 2, ' Barry '} {1, 2, ' Barry '}< /c10>
2, the increase of the collection.
Set1 = {‘Alex‘,‘Wusir‘,‘Ritian‘,‘Egon‘,‘Barry‘}set1.add ( ' scene goddess ")
directly increases the string, the position is variable print (SET1) #update: Iterate increments
Insert into each element of the collection separately set1.update ( ' a ' ) print (Set1) set1.update (" teacher ' ) print ( SET1) set1.update ([1,2,3]) print (set1)
3, delete the collection.
Set1 = {‘Alex‘,‘Wusir‘,‘Ritian‘,‘Egon‘, "barry } set1.remove ( ' alex") # delete an element print< Span style= "COLOR: #000000" > (Set1) Set1.pop () # delete an element randomly Span style= "COLOR: #0000ff" >print (Set1) set1.clear () #< Span style= "COLOR: #008000" > emptying 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}# {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}# {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}# {1, 2, 3, 6, 7, 8}
4.5 Subsets and Hyper-sets
Set1 = {}set2 = {1,2,3,4,5,6}Print (Set1 < Set2)print (Set1.issubset (Set2)) # Both of these are the same, indicating that Set1 is a subset of Set2. print (Set2 > set1)# These two are the same, both of which illustrate that 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.
Press CTRL + C to copy the code<textarea style="width: 674px; height: 233.2px; font-family: Courier New; font-size: 12px; line-height: 1.5"></textarea>Press CTRL + C to copy the code
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 ()# [1, 2, 3, [' Barry ', ' Alex ']] 2380296895816# [1, 2, 3, [' Barry ', ' Alex ']] 2380296895 048
L1[1] = 222
Print (L1,id (L1)) # [1, 222, 3, [' Barry ', ' Alex ']] 2593038941128
Print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2593038941896
'wusir'print (L1,id (l1[3)) # [1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016# [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.
ImportCOPYL1 = [1,2,3,[‘Barry‘,‘Alex‘]]L2 =Copy.deepcopy (L1)Print (L1,id (L1))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167816Print (L2,id (L2))# [1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[1] = 222print (L1,id (L1)) # [1, 222, 3, [' Barry ', ' Alex ']] 2915377167816print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[3][0] = ' wusir" # [1, 222, 3, [' Wusir ', ' Alex ']] 2915377167240print (L2,id (l2[3])) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167304
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