Python3-笔记-B-007-数据结构-集合set

# 集合(无重复元素的无序容器)
def sets():
# 集合存放不同可哈希的对象,常用于删除重复项, 做交集 / 并集 / 差集 等数学运算
# set可变, frozenset不可变,frozenset可存入set(所以修改方法是set特有的)

# --- set ---
# 创建
s = set('chschs') # 可以修改的set, {'h', 's', 'c'}
t = frozenset('bookshopa') # 不可须该的set, frozenset({'k', 'p', 'o', 'b', 's', 'a', 'h'})
r = set([1, 2, 3, 34, 15, 25, 35, 45, 75]) #列表转化到set, {1, 2, 3, 34, 35, 75, 45, 15, 25}
t = {} # set, {}
o = set([]) # set, set()
sets = set(range(10)) # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

# 获取
sets4 = sets.copy() # 浅拷贝, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

# 统计
length = len(sets) # 10

# 添加
s.add("A") # {'c', 's', 'A', 'h'}
s.add(frozenset([5, 6, 7])) # 集合中添加不可变集合, {'s', 'h', frozenset({5, 6, 7}), 'c', 'A'}

# 遍历
for i in s:
print(i)

# 删除
s.remove("A") # 不存在抛KeyErrory异常, {'s', 'h', frozenset({5, 6, 7}), 'c'}
s.discard("A") # 移除元素,不存在不抛异常,推荐使用, {'s', 'h', frozenset({5, 6, 7}), 'c'}
elem = s.pop() # 从左边弹出元素, {'h', frozenset({5, 6, 7}), 'c'}, elem='s'
s.clear()

# 数学运算
# 并集:以属于A或属于B的元素为元素的集合成为AB的并(集)
# 交集:以属于A且属于B的元素为元素的集合成为AB的交(集)
# 差集:以属于A而不属于B的元素为元素的集合成为AB的差(集)
# 对称差集:去掉两个集合里共有的元素,剩下的组合在一起
list_1 = [1, 2, 3, 4, 6, 3, 2, 5, 7, 8, 2, 1]
list_1 = set(list_1) # {1, 2, 3, 4, 5, 6, 7, 8}
list_1.add(999) # {1, 2, 3, 4, 5, 6, 7, 8, 999}
list_2 = set([2, 3, 444, 5, 1]) # {1, 2, 3, 5, 444}
s1 = list_1.intersection(list_2) # 交集 (新集合的元素来自所有集合共有的) {1, 2, 3, 5}
s2 = list_2 & list_1 # {1, 2, 3, 5}
s1 = list_1.union(list_2) # 并集 (新集合元素来自于所有集合) {1, 2, 3, 4, 5, 6, 7, 8, 999, 444}
s2 = list_2 | list_1 # {1, 2, 3, 4, 5, 6, 7, 8, 999, 444}
s1 = list_1.difference(list_2) # 差集(1里有但2里没有的) {4, 6, 999, 8, 7}
s3 = list_1 - list_2 # {4, 6, 999, 8, 7}
s2 = list_2.difference(list_1) # 2里有但1里没有的){444}
s4 = list_2 - list_1 # {444}
s1 = list_1.symmetric_difference(list_2)# 对称差集 {4, 6, 7, 8, 999, 444}
s2 = list_1 ^ list_2 # {4, 6, 7, 8, 999, 444}
s3 = list_2 ^ list_1 # {4, 6, 7, 8, 999, 444}


# 判断
n1 = set([2, 3, 4, 5])
n2 = set([4, 5, 6, 7])
n3 = set([5, 3])
boolean = 3 in n1 # t
boolean = 3 not in n2 # t
boolean = n2.isdisjoint(n1) # 两个集合没有重复元素(没有交集)则返回tf
boolean = n3.issubset(n1) # n3n1的子集则返回True, t
boolean = n3 <= n1 # issubset, t
boolean = n3 < n1 # (真子集True) sets <= arg and sets != arg, t
boolean = n1.issuperset(n3) # n1n3的父集则返回True, t
boolean = n1 >= n3 # issuperset
boolean = n1 > n3 # (真超集True) sets >= arg and sets != arg, t



# 更新修改
n1 = set([2, 3, 4, 5])
n2 = set([4, 5, 6, 7])
n3 = n1.copy()
n3.update(n2) # 添加arg里的所有元素到该集合, {2, 3, 4, 5, 6, 7}
n4 = n1.copy()
n4 |= n2 # updata

li = {'s', 'd', 'Aa'}
name = {'sd', 'd', 's'}
name.intersection_update(li) # 取交集,更新原来的set集合, {'s', 'd'}
li = {'s', 'd', 'Aa'}
name = {'sd', 'd', 's'}
li &= name # intersection_update, {'d', 's'}

li = ('s', 'd')
name = {'sd', 'd', 's'}
name.difference_update(li) # 删除当前set中的所有包含在 new set 里的元素, {'sd'}
li = {'s', 'd'}
name = {'sd', 'd', 's'}
li -= name # difference_update, 结果是空set()

name = {'sd', 'd', 's'}
li = {'s', 'd', '222'}
name.symmetric_difference_update(li) # 所有集合中非共有的元素 {'222', 'sd'}
li = {'s', 'd', '222'}
name = {'sd', 'd', 's'}
li ^= name # {'sd', '222'}
posted @ 2017-10-25 17:17  魏桐  阅读(138)  评论(0编辑  收藏  举报