python set

set

a = {11,22,33}
print(dir(a))
#['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
add(self, *args, **kwargs):
a = {11,22,33}
a.add(55)
print(a)
#{33, 11, 22, 55}
clear(self, *args, **kwargs):
a = {11,22,33}
a.clear()
print(a)
#set()
copy(self, *args, **kwargs):
a = {11,22,33}
c = a.copy()
print(c)
#{33, 11, 22}
difference(self, *args, **kwargs):
a = {11,22,33}
b = {22,33,44}
print(a.difference(b))   #以b为标准
print(b.difference(a))   #以a为标准 
#{11}
#{44}
difference_update(self, *args, **kwargs):
删除当前set中的所有包含在 new set 里的元素
a = {11,22,33}
b = {22,33,44}
a.difference_update(b)    #以a为标准删除ab共有的元素
print(a)
#{11}
discard(self, *args, **kwargs):

移除元素
a = {11,22,33}
a.discard(22)
print(a)
#{33, 11}
intersection(self, *args, **kwargs):

取交集,新创建一个set
a = {11,22,33}
b = {22,33,44}
c = a.intersection(b)
print(c)
#{33, 22}
intersection_update(self, *args, **kwargs):

取交集,修改原来set
a = {11,22,33}
b = {22,33,44}
a.intersection_update(b)
print(a)
#{33, 22}
isdisjoint(self, *args, **kwargs):

如果没有交集,返回true
a = {11,22,33}
b = {22,33,44}
c = {55,66}
print(a.isdisjoint(b))
print(a.isdisjoint(c))
#False
#True
issubset(self, *args, **kwargs):

是否是子集
a = {11,22,33}
b = {22,33,44}
c = {11,22,33,55,66}
print(a.issubset(b))
print(a.issubset(c))
#False
#True
issuperset(self, *args, **kwargs):

是否是父集
a = {11,22,33}
b = {22,33,44}
c = {11,22,33,55,66}
print(b.issuperset(a))
print(c.issuperset(a))
#False
#True
pop(self, *args, **kwargs):

随机移除
a = {11,22,33}
a.pop()
print(a)
#{11, 22}
remove(self, *args, **kwargs):

移除
a = {11,22,33}
a.remove(11)
print(a)
#{33, 22}
symmetric_difference(self, *args, **kwargs):

差集,创建新对象
a = {11,22,33}
b = {22,33,44}
c = a.symmetric_difference(b)
print(c)
#{11, 44}
symmetric_difference_update(self, *args, **kwargs):

差集,改变原来
a = {11,22,33}
b = {22,33,44}
a.symmetric_difference_update(b)
print(a)
#{11, 44}
union(self, *args, **kwargs):

并集
a = {11,22,33}
b = {22,33,44}
c = a.union(b)
print(c)
{33, 22, 11, 44}
update(self, *args, **kwargs):

更新
a = {11,22,33}
b = {22,33,44}
a.update(b)
print(a)
#{33, 22, 11, 44}

 




posted @ 2016-05-23 15:07  星星by~  阅读(385)  评论(0编辑  收藏  举报