标签:car view 随机 type data 直接 差集 div upd
s = set() print(type(s)) # =><class ‘set‘>
s = {1,2,3,4,5} s.add(1) print(s) # =>{1, 2, 3, 4, 5} s.add(6) print(s) # =>{1, 2, 3, 4, 5, 6}
s = {1,2,3,4,5} s.update({1,2,6,7,8}) print(s) # =>{1, 2, 3, 4, 5, 6, 7, 8}
s = {1,2,3,4,5} s.discard(1) print(s) # =>{2, 3, 4, 5}
s = {1,2,3,4,5} s.remove(1) print(s) # =>{2, 3, 4, 5} s.remove(6) # =>报错
s = {1,2} s.pop() print(s) # =>{2} s.pop() s.pop() # =>报错
s = {1,2,3,4,5} s.clear() print(s) # =>set()
s = {1,2,3,4,5} s1 = s.copy() print(s1) # =>{1,2, 3, 4, 5}
difference:差集,差集后的结果是一个新的集合
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s3 = s1.difference(s2) print(s3) # =>{1, 2, 3} s3 = s1-s2 print(s3) # =>{1, 2, 3} s4 = s2.difference(s1) print(s4) # =>{8, 6, 7} s4 = s2-s1 print(s4) # =>{8, 6, 7}
difference_update:删除s1集合中含有s2集合中的元素,并返回给s1
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s3 = s1.difference_update(s2) print(s1) # =>{1, 2, 3}
intersection:交集,交集后的结果是一个新的集合
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s3 = s1.intersection(s2) print(s3) # =>{4, 5} s3 = s1 & s2 print(s3) # =>{4, 5} s3 = s2.intersection(s1) print(s3) # =>{4, 5} s3 = s2 & s1 print(s3) # =>{4, 5}
intersection_update:将s1和s2的交集更新给s1
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s1.intersection_update(s2) print(s1) # =>{4, 5}
union:并集
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s3 = s1.union(s2) print(s3) # =>{1, 2, 3, 4, 5, 6, 7, 8} s3 = s1 | s2 print(s3) # =>{1, 2, 3, 4, 5, 6, 7, 8}
isdisjoint:判断两个集合是否有交集,如果没有,返回True,如果有,返回False
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s3 = {8,9} print(s1.isdisjoint(s2)) # => False print(s1.isdisjoint(s3)) # =>True
issubset:判断s2是否是s1的子集,如果是,则True,or,False
s1 = {1,2,3,4,5} s2 = {4,5} print(s2.issubset(s1)) # =>True
s1 = {1,2,3,4,5} s2 = {4,5} print(s1.issuperset(s2)) # =>True
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s3 = s1.symmetric_difference(s2) print(s3) # =>{1, 2, 3, 6, 7, 8} s3 = s2.symmetric_difference(s1) print(s3) # =>{1, 2, 3, 6, 7, 8}
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s1.symmetric_difference_update(s2) print(s1) # =>{1, 2, 3, 6, 7, 8} s2.symmetric_difference_update(s1) print(s2) # =>{1, 2, 3, 4, 5}
标签:car view 随机 type data 直接 差集 div upd
原文地址:http://www.cnblogs.com/GraceZ/p/7846603.html