e3d065b5539f8e206079d9f09b2d10c.png特点:无序、元素不重复
功能:关系测试、去重
示例
>>> import tab
>>> name_set = {1,2,4,5}
>>> name_set
set([1, 2, 4, 5])
#元素不重复
>>> name_set = {1,2,4,5,2}
>>> name_set
set([1, 2, 4, 5]
#列表变成集合
>>> a=range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a=set(a)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.add(1)
>>> a.add(10)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> a.pop()
0
>>> a
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#交集 并集 差集 对称差集,
>>> x = {1,2,3,4}
>>> y = {3,4,5,6}
>>> x & y
set([3, 4])
>>> x | y
set([1, 2, 3, 4, 5, 6])
>>> x - y
set([1, 2])
>>> x ^ y
set([1, 2, 5, 6])
>>> z = {1,2,4}
>>> z.is
z.isdisjoint( z.issubset( z.issuperset(
>>> z.issubset(x)
True
>>> z.issuperset(x)
False
网友评论