Set: {}, variable data type. The elements in it must be immutable data type, unordered and not repeated. (unimportant)
The writing of set
set1 = set({1,2,3}) #set2 = {1,2,3,[2,3],{'name':'alex'}} #Wrong print(set1) #print(set2)
set = {'alex','wusir','ritian','egon','barry'}
add update
set.add("goddess") print(set) set.add("abc") print(set) set.update("abc") print(set)
Delete pop remove clear set
set.pop() #Random deletion print(set.pop()) print(set) set.remove('alex') #Delete by element print(set) set.remove('alex2') print(set) set.clear() #clear list set() print(set)
Check for
for i in set: print(i)
Intersection & intersection
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} set3 = set1 & set2 print(set3) # {4, 5} print(set1.intersection(set2)) # {4, 5}
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}
Reciprocal 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}
Difference - difference
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 - set2) # {1, 2, 3} set1 Unique print(set2 - set1) print(set1.difference(set2)) # {1, 2, 3}
Subset < issubset
Superset > issuperset
set1 = {1,2,3,} set2 = {1,2,3,4,5,6} print(set1 < set2) print(set1.issubset(set2)) # The two are the same set1 yes set2 Subset. print(set2 > set1) print(set2.issuperset(set1)) # The two are the same set2 yes set1 Superset.
Interview questions
#Duplicate removal li = [1,2,33,33,2,1,4,5,6,6] set1 = set(li) print(set1) li = list(set1) print(li)
Make the set immutable type freeze
s1 = {1,2,3} print(s1,type(s1)) #Make a set immutable frozenset Frozen s = frozenset('barry') print(s,type(s)) #Immutable type can only be checked for i in s: print(i)
copy
Assignment operation
l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) [1, 2, 3, 'a'] [1, 2, 3, 'a']
Using copy assignment
l1 = [1,2,3] l2 = l1.copy() print(l1,l2) print(id(l1),id(l2)) l2.append('a') print(l1,l2)
copy assignment and array
#first group l1 = [1,2,[4,5,6],3] l2 = l1.copy() print(l1,id(l1)) print(l2,id(l2)) #Second group l1.append('a') print(l1,l2) #Third group l1[2].append('a') print(l1,l2) print(id(l1[2])) print(id(l2[2]))
#first group [1, 2, [4, 5, 6], 3] 2371695834312 [1, 2, [4, 5, 6], 3] 2371695834440 #Second group [1, 2, [4, 5, 6], 3, 'a'] [1, 2, [4, 5, 6], 3] #Third group [1, 2, [4, 5, 6, 'a'], 3, 'a'] [1, 2, [4, 5, 6, 'a'], 3] 2371695834184 2371695834184
copy assignment and array (2)
l1 = [1,[1],2,3,4] l2 = l1[:] #first group l1[1].append('a') #l2 What is the result of? print(l2) #Second group print(l1,id(l1)) print(l2,id(l2)) print(l1[1] is l2[1])
#first group [1, [1, 'a'], 2, 3, 4] #Second group [1, [1], 2, 3, 4] 2179834995912 [1, [1], 2, 3, 4] 2179834996040 True
Assign by defining copy
import copy #Definition copy l1 = [1,2,[4,5,6],3] l2 = copys.deepcopy(l1) #first group print(l1,id(l1)) print(l2,id(l2)) #Second group l1[2].append('a') print(l1,l2)
#first group [1, 2, [4, 5, 6], 3] 1651493850184 [1, 2, [4, 5, 6], 3] 1651493850696 #Second group [1, 2, [4, 5, 6, 'a'], 3] [1, 2, [4, 5, 6], 3]
index and enumerate usage
Print sequence number and value
li = ['alex','taibai','wusir','egon'] for i in li: print(li.index(i),i) for index,i in enumerate(li,1): print(index,i)
Basic data type summary
list
Deletion process of elements in the list
Each time an element in the list is deleted, the original subscript of the list changes
The value of range does not change as the list changes
lis = [11,22,33,44,55] for i in range(len(lis)): print(i) # i = 0 i = 1 i = 2 del lis[i] print(lis) # [11,22,33,44,55] [22, 44, 55] [22, 44]
0 [22, 33, 44, 55] 1 [22, 44, 55] 2 [22, 44] 3 Traceback (most recent call last): File "E:/py/day7-1.py", line 4, in <module> del lis[i] IndexError: list assignment index out of range
Delete odd digits in an array
lis = [11,22,33,44,55] #Method 1 lis = lis[::2] print(lis) #Method two l1 = [] for i in lis: if lis.index(i) % 2 == 0: l1.append(i) lis = l1 print(lis) #Method three for i in range(len(lis)-1,-1,-1): if i % 2 == 1: print(i) del lis[i] print(lis) print(lis)
Flexible use of dictionaries
dic = dict.fromkeys([1,2,3],'Spring elder brother') print(dic) dic = dict.fromkeys([1,2,3],[]) #dic = dict.fromkeys([1,2,3],['ysg']) print(dic) # {1: [], 2: [], 3: []} dic[1].append('Yuan Jie') print(dic) dic[2].extend('Second elder brother') print(dic)
Flexible use of arrays
l1 = [] l2 = l1 l3 = l1 l3.append('a') print(l1,l2,l3)
Do not print key value pairs with k
dic = {'k1':'v1','k2':'v2','a3':'v3'} #Method 1: Dictionary dic1 = {} for i in dic: if 'k' not in i: dic1.setdefault(i,dic[i]) dic = dic1 print(dic) #Method 2: array l = [] for i in dic: if 'k' in i: l.append(i) for i in l: del dic[i] print(dic)
Can be converted to bool value
0 '' [] () {} set()
Yuanzu: if there is only one element in Yuanzu without comma, then what type is this element and what type is the Yuanzu.
#first group tu1 = (1) tu2 = (1,) print(tu1,type(tu1)) print(tu2,type(tu2)) #Second group tu1 = ([1]) tu2 = ([1],) print(tu1,type(tu1)) print(tu2,type(tu2)) #The flexible usage of Yuan Zu dic = dict.fromkeys([1,2,3,],3) dic[1] = 4 print(dic)
#first group 1 <class 'int'> (1,) <class 'tuple'> #Second group [1] <class 'list'> ([1],) <class 'tuple'> #The flexible usage of Yuan Zu {1: 4, 2: 3, 3: 3}