python copy set of shades

1. SET set, will not be repeated, disordered 
2. Copy the depth
1. Direct assignment, two variables refer to the same object.
2. shallow copy: Only copy of the first content layer Copy ()
3. deep copy: object All content will be copy
Import copy
copy.deepcopy ()


# Deduplication
= LST [1,2,4,7,2,5,3,5,6,7,3 ] 
RET = SET (LST)
 Print (RET)
 # Output: {1, 2, 3, 4, 5, 6 , 7} 

ret.add ( 1)    # in the collection is added, but does not add duplicate 
Print (RET)
 # output: {1, 2, 3, 4, 5, 6, 7} 

# in the type list becomes 
lst = List (RET)
 Print (LST)
 # output: [1, 2, 3, 4, 5, 6, 7]

 

# 1. Direct assignment, two variables point to the same object
LST = [ ' Language ' , ' mathematics ' , ' English ' , ' geographical ' , ' chemical ' ] 
LST1 = LST 
lst1.append ( ' physical ' )    # of one of them operates both change along 
Print (LST1 , LST)
 # output: [ 'language', 'mathematics',' English ',' geography ',' chemical ',' physical '] [' language ',' mathematics', 'English', 'geography', 'chemical ',' physical '] 
Print (ID (LST), ID (LST1))
 # output (Example): 1729821762184 1729821762184 # addresses are the same, description:Two variables point to the same object

 

# 2. Shallow copy: just copy the contents of the first layer copy (), create a new object
= LST1 [ ' China ' , ' America ' , ' Russia ' , ' France ' , ' Britain ' , [ ' five permanent members ' , ' and other countries ' ]] 
lst2 = lst1.copy ()       # LST1 and not lst2 an object of 
lst1.append ( ' India ' )
 Print (LST1, lst2)
 # output: [ 'China', 'America', 'Russia', 'France', 'Britain', [ 'five permanent members,' ' as well as other countries '],' India '] 
#        ['China ',' America ',' Russia ',' France ',' Britain ', [' five permanent members, '' as well as other countries']] 

Print(ID (LST1), ID (lst2))
 # output (Example): 2822165258952 2822165761352 # addresses as described: two variable is not the same object 

LST1 [ . 5] .append ( ' Indonesia ' )
 Print (LST1)      # just copy the contents of the first layer, the second layer or may have added to it in 
Print (lst2)
 # output: [the five permanent members of the 'China', 'America', 'Russia', 'France', 'Britain', [ ' country, '' as well as other countries', 'Indonesia']] 
#        [ 'China', 'America', 'Russia', 'France', 'Britain', [ 'five permanent members,' 'as well as other countries', 'Indonesia']] 


# lst2 = LST1 [:] # sections will produce new objects, 
LST1 = [ ' China ' , ' America ' ,' Russia ' , ' France ' ,' Britain ' , [ ' five permanent members ' , ' and other countries ' ]] 
lst2 = LST1 [:] 
lst2.append ( ' Australia ' )
 Print (lst2)
 Print (LST1)
 # output: [ 'China', ' United States ',' Russia ',' France ',' Britain ', [' five permanent members, '' and other countries '],' Australia '] 
#        [' China ',' America ',' Russia ',' France '' Britain ', [' five permanent members, '' as well as other countries']] 

Print (the above mentioned id (LST1), the above mentioned id (lst2))
 # output (example): 1831290561224 1831320161672 # address is not the same for me corresponds shallow copy 

lst2 [ . 5].the append ( ' Australia ' )
 Print (lst2)
Print (LST1)
 # output: [ 'China', 'America', 'Russia', 'France', 'Britain', [ 'five permanent members,' 'as well as other countries',' Australia ']] 
#        [' China ',' America ',' Russia ',' France ',' Britain ', [' five permanent members, '' as well as other countries', 'Australia']]

 

# 3 deep copy: All content will be copy of the object
Import Copy 

lst1 = [ ' Transformers ' , ' Jurassic Park Century ' , ' hip-hop ' , ' student ' , [ ' teacher ' , ' Professor ' ]] 
lst2 = copy.deepcopy (lst1)     # The lst1 thrown in depth copy all the contents, including internal copy 

LST1 [ 4] .append ( ' teacher ' )
 Print (LST1, lst2)
 # output: [ 'Transformers', 'Jurassic Park century', 'hip-hop', 'student', [ 'teacher', 'Professor', 'teacher']] 
#        [ 'Transformers', 'Jurassic Park Century', 'hip-hop', 'student', [ 'teacher', 'Professor']] 
Print(ID (LST1), ID (lst2))
 # output (Example): 1813244593736 1813274316552

 

# 1. Why have the depth of copy? 
# Because the copy is faster than the process of creating an object

 

Guess you like

Origin www.cnblogs.com/Pengdachui-1/p/10995635.html