1 #set是可变的数据类型,里面的元素是不可变的,无序,不重复 2 # set1 = {1, 'str', False, (1, 2, 3), 1, 1} 3 # print(set1) 4 5 #增add 6 # set1 = {'chuan', 'huang', 'lin', 'li'} 7 # set1.add('liu') 8 # print(set1) 9 # 10 # #update迭代元素的更新 11 # set1.update('abc') 12 # print(set1) 13 14 15 #删 16 # set1 = {'chuan', 'huang', 'lin', 'li'} 17 # print(set1.pop()) 18 # print(set1) 19 # 20 # set1.remove('huang') #按元素去删 21 # print(set1) 22 # 23 # set1.clear() #清空 24 # del set1 #删除 25 26 27 #查 28 # set1 = {'chuan', 'huang', 'lin', 'li'} 29 # for i in set1: 30 # print(i) 31 32 #集合的并集 33 # set1 = {1,2,3,4} 34 # set2 = {3,4,5,6} 35 # set3 = set1 & set2 36 # print(set3) #{3, 4} 37 # print(set1.intersection(set2)) #{3, 4} 38 39 40 #集合的交集 41 # set1 = {1,2,3,4} 42 # set2 = {3,4,5,6} 43 # set3 = set1 | set2 44 # print(set3) #{1, 2, 3, 4, 5, 6} 45 # print(set1.union(set2)) #{1, 2, 3, 4, 5, 6} 46 47 #反交集 48 # set1 = {1,2,3,4} 49 # set2 = {3,4,5,6} 50 # print(set1 ^ set2) #{1, 2, 5, 6} 51 # print(set1.symmetric_difference(set2)) #{1, 2, 5, 6} 52 53 #差集,set1和set2去除相同部分而剩下的元素 54 # set1 = {1,2,3,4} 55 # set2 = {3,4,5,6} 56 # print(set1 - set2) #{1,2} 57 # print(set1.difference(set2)) #{1,2} 58 59 #超集和子集 60 # set1 = {1,2,3,4} 61 # set2 = {1,2,3,4,5,6} 62 # print(set1 < set2) 63 # print(set1.issubset(set2)) 64 # print(set2 > set1) 65 # print(set2.issuperset(set1)) 66 67 #去除相同的元素 68 # li = [1,55,6,2,3,4,6,6,3] 69 # set1 = set(li) 70 # print(set1) 71 # li = list(set1) 72 # print(li) 73 74 75 #转换为不可变数据类型 76 # set2 = frozenset('abcd') 77 # print(set2, type(set2))