Seven, data type conversion, set collection, and depth copy

Convert str to list with split

Convert list to str with join

Convert tuple to list

tu1 = (1,2,3)

li = list(tu1) (forced transfer)

 tu2 = tuple(li) (strong turn)

Convert dic to list

c2 = list(dic)

Convert various data types to bool

0, '',[],(),{}, set() converted to bool are Fasle

 

In the process of deleting elements in a circular list or dictionary, the result may be affected, and an error may be reported.

The error message is as follows:

RuntimeError: dictionary changed size during iteration

Explanation: Runtime error: Dictionary changed size during iteration.

gather

set2 = {11,11,11,44 } set deduplication
 print (set2)

l1 = [11, 11, 22, 22, 33, 33, 33, 44] List deduplication 
l2 = list(set(l1))
l2.sort()
print(l2)

增(add,update)

set1 = { ' alex ' , ' WuSir ' , ' RiTiAn ' , ' egon ' , ' barry ' } increase by element (can't slice, can't index, can't be long)
set1.add('zhangyajie')
print(set1)
set1 = {'alex', 'WuSir', 'RiTiAn', 'egon', 'barry'} iteratively add 

set1.update(['asd'])
print(set1)

删(pop,remove,clear,del)

set1 = { ' alex ' , ' WuSir ' , ' RiTiAn ' , ' egon ' , ' barry ' }
 print (set1.pop()) #Randomly delete
set1.remove('alex') #Remove element by element 
print(set1)

set1.clear() #Empty the list 
print(set1)

del set1 #delete the list 
print(set1)

Check (for)

set1 = {'alex', 'WuSir', 'RiTiAn', 'egon', 'barry'}
li = []
for i in set1:
    if i.startswith('a') or i.endswith('x'):
        li.append(i)
        print (li)

 

 

 

intersection (intersetion and &)

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set3 = set1 & set2
print(set3)
print(set1.intersection(set2))

union | and union

print(set1 | set2)
print(set1.union(set2))

差集 difference -

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8 }
 print (set1.difference(set2)) # unique to set1 
print (set2.difference(set1)) # unique to set2

print(set1 - set2) #unique to set1 
print(set2 - set1) #unique to set2
 
Contrast Set^ and symmetric_difference
print(set1 ^ set2)
print(set1.symmetric_difference(set2))

Subset

set1 = {1,2,3}
set2 = {1,2,3,4,5,6 }
 print (set1 > set2) # set1 is a subset of set2

superset

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5, 6}
print(set2.issuperset(set1))

freeze set

set1 = frozenset({1,2,3, 'alex'})
print(set1,type(set1))

 

Memory address comparison for assignment operations:

For assignment operations, the pointers to the same memory address are changed.

l1 = [1, 2, 3]
l2 = l1
l3 = l2
l3.append(666)
print(id(l1))
print(id(l2))
print(id(l3))
print(l3)
print(l2)
print(l1)

 

Shades of copy

Shallow copy

For shallow copy, the first layer of memory address is to create a new memory address (meaning that the two memory addresses are different), and the second layer begins, sharing the same memory address, 
so for the third, fourth, fifth, and sixth After that, the same memory address is shared.
l1 = [11,22,33 ] l2 = l1.copy() l1.append(666) print(id(l1)) print(id(l2)) print(l1) print(l2)

li = [11, 22, ['barry', [222]]] 
l1 = li.copy ()
print (id (li))
print (id (l1))
print (id (li [-1]))
print (id (l1 [-1]))

 deep copy

l1 = [11, 22, 33]
l2 = copy.deepcopy(l1)
l1.append(666)
print(l1, id(l1))
print(l2, id(l2))
result:
[11, 22, 33, 666] 233294746056
[11, 22, 33] 233294657800


Deep copy is to create a completely independent memory space. 
import copy
l1 = [11, 22, ['barry']]
l2 = copy.deepcopy(l1)
l1[2].append('alex')
print(l1,id(l1))
print(l2,id(l2 ))
print(l1, id(l1[-1]))
print(l2, id(l2[-1]))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325637346&siteId=291194637