标签:highlight 必须 个数 集合 函数 差集 card upd 大括号
集合(set)是一个无序的不重复元素序列。
可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
下面列出一些常见的操作:
"""
set.add(x) 将元素x添加到集合里
"""
s = {1, 2, 3, 4, 5, }
s.add(‘5‘)
print(s) # {1, 2, 3, 4, 5, ‘5‘}
"""
et.update(x) 将x添加到集合中,且参数可以是列表、元组、字典等
"""
s = {‘a‘, ‘cc‘, ‘f‘}
_list = [‘w‘, ‘a‘, 1]
_dict = {‘name‘: ‘bb‘, ‘age‘: ‘cc‘, ‘f‘: 11}
s.update(_dict)
print(s) # {‘a‘, ‘f‘, ‘age‘, ‘cc‘, ‘name‘}
s.update(_list)
print(s) # {‘a‘, ‘f‘, 1, ‘age‘, ‘cc‘, ‘w‘, ‘name‘}
"""
set.remove(x) 移除集合中元素,如果移除的元素不在集合中将发生错误
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s.remove(‘cc‘)
print(s) # {‘a‘, ‘f‘}
"""
set.discard(x) 移除集合中元素,如果移除的元素不在集合中不会发生错误
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s.discard(‘mm‘) # 不会报错
print(s) # {‘cc‘, ‘f‘, ‘a‘}
"""
set.pop() 随机删除集合中元素
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s.pop()
print(s) # 输出不确定是哪两个元素
"""
set.clear() 清空集合
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s.clear()
print(s) # set()
"""
len(s) 计算集合元素的个数
"""
s = {‘a‘, ‘cc‘, ‘f‘}
print(len(s))
"""
差集(-),或者使用set.difference(s)
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s1 = {‘a‘, ‘f‘, 1, ‘ww‘}
# 两种求差集的方法
print("在s中不在s1中: " + str(s.difference(s1))) # {‘cc‘}
print(‘在s1中不在s中: ‘+ str(s1 - s)) # {1, ‘ww‘}
"""
交集(&),或者使用set.intersection(s)
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s1 = {‘a‘, ‘f‘, 1, ‘ww‘}
# 同时在集合s 和 s1 中的元素
print(s.intersection(s1)) # {‘f‘, ‘a‘}
print(s1 & s) # {‘f‘, ‘a‘}
"""
并集(|),或者使用set.union(s)
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s1 = {‘a‘, ‘f‘, 1, ‘ww‘}
# 元素在集合 s 中或在集合 s1 中
print(s.union(s1)) # {‘ww‘, 1, ‘cc‘, ‘a‘, ‘f‘}
print(s1 | s) # {‘ww‘, 1, ‘cc‘, ‘a‘, ‘f‘}
"""
对称差集(^),或者使用set.sysmmetric_difference(s)
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s1 = {‘a‘, ‘f‘, 1, ‘ww‘}
# 除集合s和集合s1共有的以外的元素
print(s.symmetric_difference(s1)) # {‘ww‘, 1, ‘cc‘}
print(s1 ^ s) # {‘ww‘, ‘cc‘, 1}
"""
set.issubset(x) 判断一个集合是否是另一个集合的子集
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s1 = {‘a‘, ‘f‘}
print(s.issubset(s1)) # False
print(s1.issubset(s)) # True
"""
set.isuperset(x) 判断一个集合是否是另一个集合的父集
"""
s = {‘a‘, ‘cc‘, ‘f‘}
s1 = {‘a‘, ‘f‘}
print(s.issuperset(s1)) # True
print(s1.issuperset(s)) # False
# s1是s的子集,s是s1的父集
print(s1.issubset(s)) # True
"""
set.isdisjoint(x) 检测2个集合是否不存在交集 存在交集 False
"""
s1 = {‘ljl‘,‘wc‘,‘xy‘,‘zb‘,‘lsy‘}
s2 = {‘mmf‘,‘lsy‘,‘syj‘}
s3 = {1, 2}
print(s1.isdisjoint(s2)) # False
print(s1.isdisjoint(s3)) # True
"""
set.intersection_update(s) 用两者的交集去更新集合
"""
s1 = {‘ljl‘,‘wc‘,‘xy‘,‘zb‘,‘lsy‘}
s2 = {‘mmf‘,‘lsy‘,‘syj‘}
s1.intersection_update(s2)
s2.intersection_update(s1)
print(s1) # {‘lsy‘}
print(s2) # {‘lsy‘}
因为set的元素唯一性,所以经常用来去重,比如:在一个序列上面保持元素顺序的同时消除重复的值。
标签:highlight 必须 个数 集合 函数 差集 card upd 大括号
原文地址:https://www.cnblogs.com/yanghh/p/13193756.html