标签:清空 python3 差集 super aaa discard bin diff 一起
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:sking 4 #python3集合操作方法详解 python3集合操作大全 5 6 #集合是无序的 7 #创建集合 8 a = {1,2,3} #正确 9 b = set([1,2,3]) #正确 10 c = set((1,2,3)) #正确 11 d = set({1:3,2:4}) #结果{1, 2} 只能取字典的key 12 13 #add添加集合中的元素(添加一项) 14 a = set([1,2,3]) 15 a.add(4) 16 print(a) #{1, 2, 3, 4} 17 #update添加集合中的元素(添加多项) 18 b = set([1,2,3]) 19 b.update([4,5,6]) 20 print(b) #{1, 2, 3, 4, 5, 6} 21 #remove删除集合中的元素(删除一项) 如果不存在就会报错 22 a = set([1,2,3]) 23 a.remove(2) 24 print(a) #{1, 3} 25 #discard删除集合中的元素(删除一项)如果不存在不会报错 26 n = set([2,3,4]) 27 n.discard(3) 28 print(n) #{2, 4} 29 #pop(随机移除一个元素) 30 n = set([2,3,4,5]) 31 n1 = n.pop() 32 print(n,n1) #{3, 4, 5} 2 33 #clear(清空所有元素) 34 name = set([‘Tom‘,‘Lucy‘,‘Ben‘]) 35 name.clear() 36 print(name) #输出:set() 37 38 #len计算集合长度 39 a = set([1,2,3]) 40 b = set([‘aaa‘, ‘bbb‘, ‘ccc‘]) 41 print(len(a), len(b)) #3 3 42 #in判断某一个元素是不是在集合中 43 a = set([1,2,3]) 44 print(1 in a) #True 45 #copy浅复制 46 a = set([1,2,3]) 47 b = a.copy() 48 print(b) #{1, 2, 3} 49 50 print(‘+_+_+_+_+_+_++_+_+_+_+_+_+_‘) 51 a = [1,2,3,1,4,5,3,6] 52 b = set(a) #去除列表a中重复的元素 53 print(b) #{1, 2, 3, 4, 5, 6} 54 print(type(b)) #<class ‘set‘> 55 56 c = set([1,2,3,4,5,6]) 57 d = set([1,2,3,8,9,0]) 58 #求交集 59 #方法1 60 print(c.intersection(d)) #{1, 2, 3} 61 #方法2 62 print(c & d) #{1, 2, 3} 63 #求并集(就是都放在一起,相同的仅保留一个) 64 #方法1 65 print(c.union(d)) #{0, 1, 2, 3, 4, 5, 6, 8, 9} 66 #方法2 67 print(c | d) #{0, 1, 2, 3, 4, 5, 6, 8, 9} 68 #求差集(就是求1里面有而2里面没有的,也就是去掉1里面和2相同的元素) 69 #方法1 70 print(c.difference(d)) #{4, 5, 6} 71 #方法2 72 print(c - d) #{4, 5, 6} 73 74 #对称差集(就是把两个集合中不相同的元素提取出来) 75 c = set([1,2,3,4,5,6]) 76 d = set([1,2,3,8,9,0]) 77 #方法1 78 print(c.symmetric_difference(d)) #{0, 4, 5, 6, 8, 9} 79 #方法2 80 print(c ^ d) #{0, 4, 5, 6, 8, 9} 81 82 #判断子集和父集 83 a = set([1,2,3]) 84 b = set([1,2,3,4,5,6]) 85 #判断a是否是b的子集 86 print(a.issubset(b)) #True 87 #判断b是否是a的父集 88 print(b.issuperset(a)) #True 89 #判断两个集合是否有交集,没有交集就返回True,有交集就返回False 90 a = set([1,2,3]) 91 b = set([4,5,6]) 92 c = set([1,4,5]) 93 print(a.isdisjoint(b)) #True 94 print(a.isdisjoint(c)) #False 95 96 """ 97 add(增加元素) 98 name = set([‘Tom‘,‘Lucy‘,‘Ben‘]) 99 name.add(‘Juny‘)print(name) #输出:{‘Lucy‘, ‘Juny‘, ‘Ben‘, ‘Tom‘} 100 101 clear(清空所有元素) 102 name = set([‘Tom‘,‘Lucy‘,‘Ben‘]) 103 name.clear() 104 print(name) #输出:set() 105 106 copy(拷贝set集合) 107 name = set([‘Tom‘,‘Lucy‘,‘Ben‘]) 108 new_name = name.copy()print(new_name) #输出:{‘Tom‘, ‘Lucy‘, ‘Ben‘} 109 110 difference(返回两个或多个集合中不同的元素,并生成新的集合) 111 A = set([2,3,4,5]) 112 B = set([3,4]) 113 C = set([2]) 114 n = n1.difference(n2,n3) 115 print(n) #输出:{5}#返回A集合里面,在B和C集合中没有的元素,并生成新的集合 116 117 difference_update(删除A集合里面,在B集合中存在的元素。) 118 A = set([2,3,4,5]) 119 B = set([4,5]) 120 A.difference_update(B) 121 print(A) #输出:{2, 3} 122 123 discard(移除元素) 124 n = set([2,3,4]) 125 n.discard(3) 126 print(n) #输出:{2, 4} 127 128 intersection(取交集,并生成新的集合) 129 n1 = set([2,3,4,5]) 130 n2 = set([4,5,6,7]) 131 n = n1.intersection(n2) 132 print(n) #输出:{4, 5} 133 134 intersection_update(取交集,修改原来的集合) 135 n1 = set([2,3,4,5]) 136 n2 = set([4,5,6,7]) 137 n1.intersection_update(n2) 138 print(n1) #输出:{4, 5} 139 140 isdisjoint(判断交集,是返回False,否返回True) 141 n1 = set([2,3,4,5]) 142 n2 = set([4,5,6,7]) 143 print(n1.isdisjoint(n2)) #输出:False 144 145 issubset(判断子集) 146 A = set([2,3]) 147 B = set([2,3,4,5]) 148 print(A.issubset(B)) #输出:True#A是B的子集 149 150 issuperset(判断父集) 151 A = set([2,3]) 152 B = set([2,3,4,5]) 153 print(B.issuperset(A)) # 输出:True#B是A的父集 154 155 pop(随机移除一个元素) 156 n = set([2,3,4,5]) 157 n1 = n.pop() 158 print(n,n1) # 输出:{3, 4, 5} 2 159 160 remove(移除指定元素) 161 n = set([2,3,4,5]) 162 n.remove(2) 163 print(n) # 输出:{3, 4, 5} 164 165 symmetric_difference(取交集,并生成新的集合) 166 A = set([2,3,4,5]) 167 B = set([4,5,6,7]) 168 print(A.symmetric_difference(B)) # 输出:{2, 3, 6, 7} 169 170 symmetric_difference_update(取交集,改变原来的集合) 171 A = set([2,3,4,5]) 172 B = set([4,5,6,7]) 173 A.symmetric_difference_update(B) 174 print(A) # 输出:{2, 3, 6, 7} 175 176 union(取并集,并生成新的集合) 177 A = set([2,3,4,5]) 178 B = set([4,5,6,7]) 179 print(A.union(B)) # 输出:{2, 3, 4, 5, 6, 7} 180 181 update(取并集,改变原来的集合) 182 A = set([2,3,4,5]) 183 B = set([4,5,6,7]) 184 A.update(B) 185 print(A) # 输出:{2, 3, 4, 5, 6, 7} 186 """
标签:清空 python3 差集 super aaa discard bin diff 一起
原文地址:https://www.cnblogs.com/skings/p/10257631.html