python set

来源:互联网 发布:网络t客人来ktv 编辑:程序博客网 时间:2024/04/28 18:11
stdSet = set()#python setstdSet.add('chen')print(stdSet)#   {'chen'}stdSet.clear()print(stdSet)#   set()stdSet = set(['chen','long','fu'])print(stdSet)#   {'fu', 'long', 'chen'}print(stdSet.difference(['fu','long']))#   {'chen'}    找出不同,并生成新的set,通过返回值返回stdSet.difference_update(['fu','long'])print(stdSet)#   {'chen'}    找出不同,并更新到原setstdSet = set(['one','two','three','four'])print(stdSet)#   {'four', 'two', 'three', 'one'}stdSet.discard('one')print(stdSet)#   {'four', 'two', 'three'}stdSetA = set(['one','two','three','four'])stdSetB = set(['three','four','five','six'])stdSetC = stdSetA.intersection(stdSetB)print(stdSetC)#   {'three', 'four'}   找出两个set的不同之处并生成新setstdSetA = set([1,2,3,4])stdSetB = set([3,4,5,6])stdSetA.intersection_update(stdSetB)print(stdSetA)#   {3, 4}              找出两个set的不同之处并修改该setstdSetA = set([1,2,3,4])stdSetB = set([3,4,5,6])print(stdSetA.isdisjoint(stdSetB))#   False               判断两个set是否有交集stdSetA = set([1,2,3,4,5])stdSetB = set([1,2,3])print(stdSetA.issuperset(stdSetB))#   True    A是否是B的父集print(stdSetB.issubset(stdSetA))#   True    B是否是A的子集stdSetA = set([1,2,3,4,5,6])ret = stdSetA.pop()print(stdSetA,ret)#   {2, 3, 4, 5, 6} 1   pop移除并返回值s1 = set([1,2,3,4])s2 = set([3,4,5,6])print(s1.difference(s2))#   {1, 2}              取s1中的差集print(s1.symmetric_difference(s2))#   {1, 2, 5, 6}        取总差集
0 0
原创粉丝点击