This article is mainly for everyone in-depth analysis of the Python3 set introduction, with a certain reference value, interested in small partners can refer to
# Auther:aaron Fan ' ' collection is an unordered, non-repeating combination of data that has the main function of: go to the weight, turn a list into a set, and automatically go back to the relationship test, test the intersection of two sets of data, difference set, and the relationship between the set of "list_1 = [ 1,3,4,7,3,6,7,9] #去重list_1 = set (list_1) list_2 = set ([2,6,0,66,22,8,4]) List_3 = set ([1,3,7]) print (list_1) print (list_2 Print (list_3) print ("---------------------------") #关系测试 # intersection (two columns with values, here 4, 6): Print (List_1.intersection (list_2 ) #并集 (combine two columns and then go to weight): print (List_1.union (list_2)) #差集 (remove the list_1 inside and list_2): print (list_1.difference 2) #对称差集 (two lists, no one is taken out of each other, that is, to remove only those values) print (List_1.symmetric_difference (list_2)) #子集 (to determine whether list_1 contains List_ 3 all values inside) print (List_3.issubset (list_1)) #父集 (determine if list_1 is the parent set of List_3) print (List_1.issuperset (list_3)) #无交集 (Judging List_ Whether 3 and List_4 have no intersection at all) List_4 = set ([5,6,8]) print (List_3.isdisjoint (list_4)) # Another way to-----------------------relationship test: ' s = Set ([3,5,9,10]) #创建一个数值集合t = set ("Hello") #创建一个唯一字符的集合a = T | S # T and S of the set B = t & S # T and s intersection c = t–s # differential Set (item in T, but not in s) d = t ^ S # symmetric difference set (item in T or s) , but not both) basic operation: T.add (' x ') # Add an item s.update ([10,37,42]) # Add multiple items in S use Remove () to delete an item: T.remove (' h ') #有就删除, without an error t.pop () #随机弹出一个t. Discard (' h ') #有就删除, No, not an error. Len (s) sets the length of x in S to test if X is a member of S X not in S to test if X is not a member of S S.issubset (t) s <= t tests whether every element in S is in T S.issuperset (t) S >= T Tests if every element in T is in S s.union (t) s | T returns a new set containing each element in S and T S.intersection (t) S & T Returns a new set containing the common elements in S and T s.difference (t) s-t returns a new set containing S but T The element S.symmetric_difference (t) s ^ t returns a new set containing non-repeating elements in S and T S.copy () returns a shallow copy of the set "s"