def difference(self, *args, **kwargs): 
s1 = set([1,2,3,4,5])
print (s1)

s2 = set([3,4,6])
print (s2)

s3 = s1.difference(s2)
print (s3)
结果:

{1, 2, 3, 4, 5}
{3, 4, 6}
{1, 2, 5}

 结论:difference计算在s1集合中但是不在s2集合中的内容,并且生成一个新的集合s3,原来s1,s2不变

def difference_update(self, *args, **kwargs): 
s1 = set([1,2,3,4,5])
s2 = set([3,4,6])
s4 = s1.difference_update(s2)
print (s1)
print (s2)
print (s4)
结果:

{1, 2, 5}
{3, 4, 6}
None

 结论:difference_update计算了在s1集合中但是不在s2集合中的内容,并且更新原有的集合s1,没有生成新的集合(s4=None)

def intersection(self, *args, **kwargs):
s1 = set([1,2,3,4,5])
s2 = set([1,2,6,7])

s3 = s1.intersection(s2)
print (s3)
print ("==================")
print (s1)
print (s2)
结果:

{1, 2}
==================
{1, 2, 3, 4, 5}
{1, 2, 6, 7}

 结论:取交集

def symmetric_difference(self, *args, **kwargs):
s1 = set([1,2,3,4,5])
s2 = set([1,2,6,7])

s3 = s1.symmetric_difference(s2)
print (s3)
print ("==================")
print (s1)
print (s2)
结果:

{3, 4, 5, 6, 7}
==================
{1, 2, 3, 4, 5}
{1, 2, 6, 7}

 结论:两个集合里面不匹配的项

 

配置库的写法:

s1 = set([1,2,3,4,5])
s2 = set([1,2,6,7])

ret_join = s1.intersection(s2)
ret_del = s1.difference(ret_join)
ret_add = s2.difference(ret_join)

print (ret_join)
print (ret_del)
print (ret_add)

{1, 2}
{3, 4, 5}
{6, 7}

 

 

 



posted on 2016-04-29 01:55  Alex0425  阅读(401)  评论(0编辑  收藏  举报