add(增加元素)
name = set(['Tom','Lucy','Ben']) name.add('Juny') print(name)#输出:{'Lucy', 'Juny', 'Ben', 'Tom'}
clear(清空所有元素)
name = set(['Tom','Lucy','Ben']) name.clear()
print(name)#输出:set()
copy(拷贝set集合)
name = set(['Tom','Lucy','Ben']) new_name = name.copy() print(new_name)#输出:{'Tom', 'Lucy', 'Ben'}
difference(返回两个或多个集合中不同的元素,并生成新的集合)
A = set([2,3,4,5]) B = set([4,5]) A.difference_update(B) print(A)#输出:{2, 3}
discard(移除元素)
n = set([2,3,4]) n.discard(3) print(n)#输出:{2, 4}
intersection(取交集,并生成新的集合)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n = n1.intersection(n2) print(n)#输出:{4, 5}
intersection_update(取交集,修改原来的集合)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n1.intersection_update(n2) print(n1)#输出:{4, 5}
isdisjoint(判断交集,是返回False,否返回True)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) print(n1.isdisjoint(n2))#输出:False
issubset(判断子集)
A = set([2,3]) B = set([2,3,4,5]) print(A.issubset(B))#输出:True#A是B的子集
issuperset(判断父集)
A = set([2,3]) B = set([2,3,4,5]) print(B.issuperset(A))# 输出:True#B是A的父集
pop(随机移除一个元素)
n = set([2,3,4,5]) n1 = n.pop() print(n,n1)# 输出:{3, 4, 5} 2
remove(移除指定元素)
n = set([2,3,4,5]) n.remove(2) print(n)# 输出:{3, 4, 5}
symmetric_difference(取交集,并生成新的集合)
A = set([2,3,4,5]) B = set([4,5,6,7]) print(A.symmetric_difference(B))# 输出:{2, 3, 6, 7}
symmetric_difference_update(取交集,改变原来的集合)
A = set([2,3,4,5]) B = set([4,5,6,7]) A.symmetric_difference_update(B) print(A)# 输出:{2, 3, 6, 7}
union(取并集,并生成新的集合)
A = set([2,3,4,5]) B = set([4,5,6,7]) print(A.union(B))# 输出:{2, 3, 4, 5, 6, 7}
update(取并集,改变原来的集合)
A = set([2,3,4,5]) B = set([4,5,6,7]) A.update(B)print(A)# 输出:{2, 3, 4, 5, 6, 7}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119629.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...