python 5.7

1. Supplement of basic data types

#Supplement of data type 
# tuple, if there is one element in it and there is no comma, the data type is the type of the only element.
#tu = ([1,2,3]) #tu has only one list, no commas. The data type is list.
# print(tu,type(tu))
# tu = ([1,2,3],) #Although there is only one element in it, there is a comma after it. Still the ancestor type.
# print(tu,type(tu))
#For loop, it is best not to delete elements in the list or dictionary, and change the size of the list and dictionary. will go wrong.
#Solution, list, reverse loop, this will not change the size of the list.
# li = ['liujj','xiaol','jisp'] # for i in range(len(li)-1,-1,-1): #range does not care about the head, so the tail is 0-1= -1, len() is 1 more than the index, so len()-1, when len() is the tail,
# if i % 2 == 1: #If the loop starts from 0, len() does not need -1 .
# li.pop(i)
# print(li) #Solution
, dictionary, first put the keys to be deleted into a list, then loop the list and delete the dictionary.
# dic = {'k1':'v1','k2':'v2','k3':'v3','name':'alex'}
# lis = [] # for i in dic:
# if '

# for m in lis:
# dic.pop(m)
# print(dic)
#tuple-->list list(tu) tuple(list)
#dic -->list list(dic): all keys---> list(dic.keys())
# All values--->list(dic.values())
# All key-value pairs (the list is a pair of tuple)--->list(dic.items ())
#set-->list list(set())
#dict.fromkeys(the elements before splitting, the unique value corresponding to each element)
# dic = dict.fromkeys('abc',66)
# print (dic)
#False: 0 , '' , [] , {} , ()

Second, set
#Adding collections 
# 1.add # set = {'liujj','jiasp','jiaxl'} 
# set.add('cat') 
# print(set) 
# 2.update Split first and then add 
# set 
. update('abcde') 
# print(set) #Deletion of a set 
# 1.remove Delete by element 
# set.remove('jiaxl') 
# print(set) 
# 
2.pop Randomly delete, the return value is the deleted element. 
# print(set.pop()) 
# print(set) 
# 3.del 
# 4.clear Empty the list: set() #Check of sets 
#Only for 
one kind
Addition and deletion of collections
 

3. The relationship of the set

# Intersection of sets: & intersection 
# Union of sets: | union 
# Difference of sets: -difference 
# Anti-intersection of sets: ^symmetric_difference 
# Subset of sets: < Returns true or false. issubset #Superset 
of collection: > issuperset
Intersection and Difference Anti-Intersection Subset Superset

Four, depth copy


#Deep copy # 1. Assignment. One changes and the other also changes. It must be assigned first. If the small data pool itself is the same memory address, it does not need to be assigned again. 
# li = [1 , 2 , 3] 
# li2 = li 
# li.append(99) #set2 changes with set1 
# print(li,li2) 
# 2. Shallow copy. The first layer is independent, and the second layer and above are the same memory address, which changes accordingly. 
# li1 ​​= [1 , 2 , 3 , ['liujj','jiasp'] ] 
# li2 = li1.copy() 
# li1[3].append('jiaxl') #The second layer is added, so in li2 The second layer is also added 
# print(li1,li2) 
# li1.append(4) 
# print(li1,li2) 
# 3. Deep copy, import copy / copy.deepcopy(table name). #No matter how many layers is completely independent. 
# import copy 
#li2 = copy.deepcopy(li1) 
# li1.append(77) 
# print(li1,li2) 
# li1[3].append('xial') 
# print(li1,li2) 
# 4. The slice of the list also belongs to- ----> Shallow copy. 
# li2 = li1[:] 
# li1.append(55) 
# print(li1,li2) 
# li1[3].append('lang') 
# print(li1,li2)
Shades of copy

 

Guess you like

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