python-set

#!/usr/bin/python
# -*- coding:utf-8 -*-
#集合的交集/并集/差补/对称差分
a=set((1,2,3,"a","b"))
b=set((1,2,4,"a","c"))

print   a & b                       #交集:等同于a.interseciton(b)
print   a | b                       #补集:等同于a.union(b)
print   a-b                         #差补:d等同于a.diffrence(b)在a集合中去掉所有与b集合中相同的元素                                                                  //          只在a中保留b中没有的结合元素
print   a^b                         #对称差分:找出两个结合中,只属于集合a或者集合b的元素





运行结果:


orangemarmot ODW [:~/python/python-scripts] 1206$ python set.py
set(['a', 1, 2])
set(['a', 1, 2, 3, 4, 'c', 'b'])
set([3, 'b'])
set(['c', 3, 4, 'b'])

==========

>>> a=set((1,2,3,"a","b"))
>>> set(a)
set(['a', 1, 2, 3, 'b'])
>>> b=set((1,2,4,"a","c"))
>>> a
set(['a', 1, 2, 3, 'b'])
>>> b
set(['a', 1, 2, 4, 'c'])
>>> a & b
set(['a', 1, 2])
>>> a.intersection(b)
set(['a', 1, 2])
>>> a | b
set(['a', 1, 2, 3, 4, 'c', 'b'])
>>> a.union(b)
set(['a', 1, 2, 3, 4, 'c', 'b'])
>>> a-b
set([3, 'b'])
>>> a^b
set(['c', 3, 4, 'b'])




你可能感兴趣的:(自动化,python)