python set
2018-05-17 本文已影响0人
little_short
可变集合set 集合对象是一组无序排列的可哈希的值,集合成员可以做字典的键
s = set('12313 2')
print(s)
s1 = [1,2,3,1]
print(list(set(s1)))
报错
li = [[1,2],1,2]
set(li)
不可变集合 frozenset()
li = [1,'a','b']
s = set(li)
dic = {s:'123'}
集合取值/判断
遍历取值
li = [1,'ab','b']
s = set(li)
s.add('www')
print(s)
s.update([12,2])
print(s)
s.remove(1)
print(s)
s.pop()
print(s)
s.clear()
print(s)
清空 为空大空号都没了和删除不一样
in
li = [1,'a','b']
s = set(li)
print(2 in li)
not in
集合等价不等价
s = set('alex') == set('alexexex')
包含
print(set('alex') < set('alexw'))
a = set([1, 2, 3, 4, 5])
b = set([4, 5, 6, 7, 8])
交集
s = a.intersection(b)
s = a&b
print(s)
并集 |
s1 = a.union(b)
s1 = a | b
print(s1)
差集 a里面有b里面没有 -
s2 = a.difference(b)
s2 = a - b
print(s2)
对称差集
s3 = a.symmetric_difference(b)
s3 = a ^ b
print(s3)
父级 超集 >
print(a.issuperset(b))
子集 <
print(a.issubset(b))
短路
a = set([1, 2, 3, 4, 5, 9])
b = set([4, 5, 6, 7, 8])
print(a and b)
print(a or b)