* 集合概念
元素没有序列,但元素不能重复
元素的数据类型可以是数字、字符串、元组,不能是列表和字典
* 常规操作
# 创建
>>> myset=set("hello")
>>> type(myset)
Out[137]: set
>>> myset
Out[138]: {'e', 'h', 'l', 'o'}
>>> myset=set(['moring','midday','night']) #列表
>>> myset
Out[140]: {'midday', 'moring', 'night'}
>>> myset=set(('moring','midday','night')) #元组
>>> myset
Out[169]: {'midday', 'moring', 'night'}
>>> myset={'moring','midday','night'}
>>> type(myset)
Out[144]: set
>>> myset
Out[145]: {'midday', 'moring', 'night'}
小心看和字典不一样,没有key
>>> myset={'moring','midday','night','night'}
>>> myset
Out[161]: {'midday', 'moring', 'night'}
元素不可能重复,会自动过滤
# 查看
本身不能支持索引读,只能先转为list,再读:
>>> myset
Out[201]: {'midday', 'moring', 'night'}
>>> mylist = list(myset)
>>> mylist
Out[203]: ['moring', 'midday', 'night']
>>> mylist[0]
Out[204]: 'moring'
# 遍历
>>> myset={'moring','midday','night'}
>>> for item in myset:
... print item
...
moring
midday
night
# 增加
>>> myset.add('day')
>>> myset
Out[171]: {'day', 'midday', 'moring', 'night'}
# 合并
>>> myset={'moring','midday','night'}
>>> myset2={'day','week'}
>>> myset.update(myset2)
>>> myset
Out[182]: {'day', 'midday', 'moring', 'night', 'week'}
>>> myset2
Out[183]: {'day', 'week'}
# 删除
>>> myset
Out[184]: {'day', 'midday', 'moring', 'night', 'week'}
>>> myset.pop()
Out[185]: 'moring'
>>> myset
Out[186]: {'day', 'midday', 'night', 'week'}
任意选择一个删除
>>> myset
Out[186]: {'day', 'midday', 'night', 'week'}
>>> myset.remove("night")
>>> myset
Out[188]: {'day', 'midday', 'week'}
指定元素删除,没有的键就报错
>>> myset
Out[191]: {'day', 'midday', 'week'}
>>> myset.discard('week')
>>> myset
Out[193]: {'day', 'midday'}
指定元素删除,比remove安全,没有的就返回null
>>> myset
Out[193]: {'day', 'midday'}
>>> myset.clear()
>>> myset
Out[195]: set()
清除所有的元素
# 冻结
>>> myset
Out[205]: {'midday', 'moring', 'night'}
>>> myset=frozenset(myset)
>>> myset
Out[207]: frozenset({'midday', 'moring', 'night'})
>>> myset.add('day')
Traceback (most recent call last):
不再增加了
# 集合操作
@ 元素是否为集合中
>>> myset1
Out[216]: {2, 4, 5, 6, 8}
>>> 2 in myset1
Out[218]: True
@是否相等
>>> myset1
Out[219]: {2, 4, 5, 6, 8}
>>> myset2
Out[220]: {2, 3, 6, 9}
>>> myset1 == myset2
Out[221]: False
>>> myset1!=myset2
Out[222]: True
@是否为子集
>>> myset1 < myset2
Out[223]: False
>>> myset1.issubset(myset2)
Out[224]: False
>>> myset1.issuperset(myset2)
Out[225]: False
@并集
>>> myset1
Out[226]: {2, 4, 5, 6, 8}
>>> myset2
Out[227]: {2, 3, 6, 9}
>>> myset1 | myset2
Out[228]: {2, 3, 4, 5, 6, 8, 9}
>>> myset1.union(myset2)
Out[229]: {2, 3, 4, 5, 6, 8, 9}
@交集
>>> myset1
Out[230]: {2, 4, 5, 6, 8}
>>> myset2
Out[231]: {2, 3, 6, 9}
>>> myset1 & myset2
Out[232]: {2, 6}
>>> myset1.intersection(myset2)
Out[233]: {2, 6}
@补集
myset1 相对于 myset2 的补集
>>> myset1
Out[240]: {2, 4, 5, 6, 8}
>>> myset2
Out[241]: {2, 3, 6, 9}
>>> myset1-myset2
Out[242]: {4, 5, 8}
>>> myset1.difference(myset2)
Out[246]: {4, 5, 8}
myset2 相对于 myset1 的补集
>>> myset1
Out[243]: {2, 4, 5, 6, 8}
>>> myset2
Out[244]: {2, 3, 6, 9}
>>> myset2-myset1
Out[245]: {3, 9}
>>> myset2.difference(myset1)
Out[247]: {3, 9}
@对称差集
就是交集剩下来的,
也可以理解为 两个集合相互补集的和
>>> myset1
Out[248]: {2, 4, 5, 6, 8}
>>> myset2
Out[249]: {2, 3, 6, 9}
>>> myset1.symmetric_difference(myset2)
Out[250]: {3, 4, 5, 8, 9}
>>> myset2.symmetric_difference(myset1)
Out[251]: {3, 4, 5, 8, 9}
>>> myset1.union(myset2)-myset1.intersection(myset2)
Out[252]: {3, 4, 5, 8, 9}
* 查集合的帮助文档
dir(set) help(set.xxx) xxx具体的方法名
打开字典对象文档 print set.__doc__