标签:cti 合集 name union 更新 set 方法 class sdi
去重并创建集合(无序)
python_1 = [‘1‘,‘2‘,‘3‘] linux_1 = [‘1‘,‘2‘] #python_and_linux_1 = [] #for p_name in python_1: # if p_name in linux_1: # python_and_linux_1.append(p_name) #print(python_and_linux_1) p_s = set(python_1) print(p_s)
求交集
python_1 = [‘1‘,‘2‘,‘3‘] linux_1 = [‘1‘,‘2‘] #python_and_linux_1 = [] #for p_name in python_1: # if p_name in linux_1: # python_and_linux_1.append(p_name) #print(python_and_linux_1) p_s = set(python_1) l_s = set(linux_1) print(p_s,l_s) print(p_s.intersection(l_s)) print(p_s&l_s)#同上述函数
求并集
python_1 = [‘1‘,‘2‘,‘3‘] linux_1 = [‘1‘,‘2‘,‘4‘] #python_and_linux_1 = [] #for p_name in python_1: # if p_name in linux_1: # python_and_linux_1.append(p_name) #print(python_and_linux_1) p_s = set(python_1) l_s = set(linux_1) print(p_s,l_s) print(p_s.union(l_s)) print(p_s|l_s)
求差集
python_1 = [‘1‘,‘2‘,‘3‘] linux_1 = [‘1‘,‘2‘,‘4‘] #python_and_linux_1 = [] #for p_name in python_1: # if p_name in linux_1: # python_and_linux_1.append(p_name) #print(python_and_linux_1) p_s = set(python_1) l_s = set(linux_1) print(p_s,l_s)
print(p_s.difference(l_s))
print(p_s-l_s)
集合其他方法
交叉补集 并集-交集
python_1 = [‘1‘,‘2‘,‘3‘] linux_1 = [‘1‘,‘2‘,‘4‘] #python_and_linux_1 = [] #for p_name in python_1: # if p_name in linux_1: # python_and_linux_1.append(p_name) #print(python_and_linux_1) p_s = set(python_1) l_s = set(linux_1) print(p_s,l_s) print(p_s.symmetric_difference(l_s)) print(p_s^l_s)
difference_update
p_s=p_s-l_s
==p_s.difference_update(l_s)
同理 intersection_update
isdisjoint 两个合集无共同元素
返回布尔值
issubset,s1包含于s2,子集
相当于s1<s2
返回布尔值
python_1 = [‘1‘,‘2‘,‘3‘] linux_1 = [‘1‘,‘2‘,‘4‘] p_s = set(python_1) l_s = set(linux_1) print(p_s,l_s) print(p_s.issubset(l_s))
issuperset,父集
update更新元素,add只能添加一个元素
s1={1,2} s2={1,2,3,4} s1.update(s2) print(s1)
标签:cti 合集 name union 更新 set 方法 class sdi
原文地址:https://www.cnblogs.com/wangleiyifan/p/9241325.html