用途: 关系运算去重复
表示形式
a={1,2,3,2,'n'}
print(a)#去重成功,但是乱序
{1, 2, 3, 'n'}
len长度
print(len(a))
4
in成员运算
print(f"3 in a:{3 in a}")
3 in a:True
|并集
a={'7','4','4','fdfk'}
b={'7',"fdfk",'3'}
print(f"a|b:{a|b}")
print(f"答案是:{a|b}")
a|b:{'fdfk', '7', '3', '4'}
答案是:{'fdfk', '7', '3', '4'}
&交集
print(f"{a&b}")
print(f"a&b:{a&b}")
{'fdfk', '7'}
a&b:{'fdfk', '7'}
-差集
print(f"{a - b}")
print(f"b-a:{b - a}")
{'4'}
b-a:{'3'}
^对称差集
print(f"a^b:{a ^ b}")
a^b:{'4', '3'}
==判断
c = {'dsd', '4', 'e'}
print(b == c)
False
>父集
a > b
d = {'dsd', '4', 'e', '3'}
print(d >= c)
True
<子集
print(d <= c)
False
add添加
s = {12, 'i'}
s.add(0)
print(s)
{0, 12, 'i'}
remove删除
s.remove(12)
print(s)
{0, 'i'}
difference_update移除两个集合中相同的元素
a = {'7', '4', '4', 'fdfk'}
b = {'7', "fdfk", '3'}
a.difference_update(b)
print(a)
print('*' * 20)
a = {'7', '4', '4', 'fdfk'}
b = {'7', "fdfk", '3'}
b.difference_update(a)
print(b)
{'4'}
********************
{'3'}
discard移除
s = {'f', 'gsd', 1}
s.discard(2) # 如果使用remove指令,则会报错
print(s)
{1, 'f', 'gsd'}
isdisjoint检查是否有交集
a = {'a', 'd'}
b = {'a', 8, 9, 0}
print(a.isdisjoint(b))
False
标签:fdfk,update,remove,print,12,集合,difference
From: https://www.cnblogs.com/nickyu511/p/17999737