python collection, copy depth

A collection of

Sets are unordered, unique data set that is inside the hash element (immutable type), but the hash itself is not set (it is set dictionary do not bond). The following is a collection of the most important points:

  De-emphasis, the list becomes a collection, automatically go heavier.

  Relationship test, before the intersection of two sets of test data, poor, union and other relations.

1, to create the collection.

set1 = set({1,2,'barry'})
set2 = {1,2,'barry'}
print(set1,set2)  # {1, 2, 'barry'} {1, 2, 'barry'}

2, increasing the collection.

 set1 = {'alex','wusir','ritian','egon','barry'}
set1.add ( 'View Goddess') 
Print (setl) 

#update: Iterative increased 
set1.update ( 'A') 
Print (setl) 
set1.update ( 'teacher') 
Print (setl) 
set1.update ([. 1, 2, 3]) 
Print (SET1)

3, delete the collection.

 
= {setl 'Alex', 'wusir', 'ritian', 'Egon', 'Barry'} 

set1.remove ( 'Alex') # removes an element 
print (setl) 

set1.pop () # delete a random element 
print (SET1) 

set1.clear () # empty set of 
print (set1) 

del SET1 # delete a collection 
print (set1)

4, other sets of operations:

  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 union. (| Or union)

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2)  # {1, 2, 3, 4, 5, 6, 7,8}

print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7,8}

  4.3 difference sets. (- 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 Anti 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 subset superset

 
l, 2,3} = {set1 
SET2 = {1,2,3,4,5,6} 

Print (set1 <SET2) 
Print (set1.issubset (SET2)) # two identical, are described set1 is set2 subset. 

Print (set2> set1) 
Print (set2.issuperset (set1)) # two identical, are explained set1 set2 is a superset.

5, frozenset immutable collections, so the set becomes immutable types.

s = frozenset('barry')
print(s,type(s))  # frozenset({'a', 'y', 'b', 'r'}) <class 'frozenset'>

 Second, copy depth

1, look at the assignment operator.

l1 = [1,2,3,['barry','alex']]
l2 = l1

l1[0] = 111
print(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', 'alex']]

For the assignment operator is, l1 and l2 points 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']] 2380296895816 print(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']] 1732315659016 print(l2,id(l2[3])) # [1, 2, 3, ['wusir', 'alex']] 1732315659016

For shallow copy, the first layer of creating a new memory address, while the second layer from the beginning, is point to the same memory address, so, for the second layer and the deeper layers, the consistency .

3, deep copy deepcopy.

import copy
l1 = [1,2,3,['barry','alex']]
l2 = copy.deepcopy(l1)

print(l1,id(l1))  # [1, 2, 3, ['barry', 'alex']] 2915377167816
print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 2915377167048

l1[1] = 222
print(l1,id(l1))  # [1, 222, 3, ['barry', 'alex']] 2915377167816
print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 2915377167048

l1[3][0] = 'wusir'
print(l1,id(l1[3]))  # [1, 222, 3, ['wusir', 'alex']] 2915377167240
print(l2,id(l2[3]))  # [1, 2, 3, ['barry', 'alex']] 2915377167304

For deep copy, the two are completely separate, change any element of any one of (no matter how many layers), another absolutely does not change.

Sets are unordered, unique data set that is inside the hash element (immutable type), but the hash itself is not set (it is set dictionary do not bond). The following is a collection of the most important points:

  De-emphasis, the list becomes a collection, automatically go heavier.

  Relationship test, before the intersection of two sets of test data, poor, union and other relations.

1, to create the collection.

set1 = set({1,2,'barry'})
set2 = {1,2,'barry'}
print(set1,set2)  # {1, 2, 'barry'} {1, 2, 'barry'}

2, increasing the collection.

 set1 = {'alex','wusir','ritian','egon','barry'}
set1.add ( 'View Goddess') 
Print (setl) 

#update: Iterative increased 
set1.update ( 'A') 
Print (setl) 
set1.update ( 'teacher') 
Print (setl) 
set1.update ([. 1, 2, 3]) 
Print (SET1)

3, delete the collection.

 
= {setl 'Alex', 'wusir', 'ritian', 'Egon', 'Barry'} 

set1.remove ( 'Alex') # removes an element 
print (setl) 

set1.pop () # delete a random element 
print (SET1) 

set1.clear () # empty set of 
print (set1) 

del SET1 # delete a collection 
print (set1)

4, other sets of operations:

  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 union. (| Or union)

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2)  # {1, 2, 3, 4, 5, 6, 7,8}

print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7,8}

  4.3 difference sets. (- 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 Anti 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 subset superset

 
l, 2,3} = {set1 
SET2 = {1,2,3,4,5,6} 

Print (set1 <SET2) 
Print (set1.issubset (SET2)) # two identical, are described set1 is set2 subset. 

Print (set2> set1) 
Print (set2.issuperset (set1)) # two identical, are explained set1 set2 is a superset.

5, frozenset immutable collections, so the set becomes immutable types.

s = frozenset('barry')
print(s,type(s))  # frozenset({'a', 'y', 'b', 'r'}) <class 'frozenset'>

 Second, copy depth

1, look at the assignment operator.

l1 = [1,2,3,['barry','alex']]
l2 = l1

l1[0] = 111
print(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', 'alex']]

For the assignment operator is, l1 and l2 points 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']] 2380296895816 print(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']] 1732315659016 print(l2,id(l2[3])) # [1, 2, 3, ['wusir', 'alex']] 1732315659016

For shallow copy, the first layer of creating a new memory address, while the second layer from the beginning, is point to the same memory address, so, for the second layer and the deeper layers, the consistency .

3, deep copy deepcopy.

import copy
l1 = [1,2,3,['barry','alex']]
l2 = copy.deepcopy(l1)

print(l1,id(l1))  # [1, 2, 3, ['barry', 'alex']] 2915377167816
print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 2915377167048

l1[1] = 222
print(l1,id(l1))  # [1, 222, 3, ['barry', 'alex']] 2915377167816
print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 2915377167048

l1[3][0] = 'wusir'
print(l1,id(l1[3]))  # [1, 222, 3, ['wusir', 'alex']] 2915377167240
print(l2,id(l2[3]))  # [1, 2, 3, ['barry', 'alex']] 2915377167304

For deep copy, the two are completely separate, change any element of any one of (no matter how many layers), another absolutely does not change.

Guess you like

Origin www.cnblogs.com/chenzxl/p/10988330.html