接下来。装逼开始….
项目中使用比较多的方法
add() 添加
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 将元素 10 添加到集合中
a.add(10)
print(a)
# 运行结果
{1, 2, 3, 4, 5, 6, 10}
Process finished with exit code 0
remove() 移除
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 从集合中移除元素 1。如果 1 不存在于集合中则会引发 KeyError
a.remove(1)
print(a)
# 运行结果
{2, 3, 4, 5, 6}
Process finished with exit code 0
pop() 删除
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 从集合中移除并返回任意一个元素。如果集合为空则会引发 KeyError
a.pop()
print(a)
# 运行结果
{2, 3, 4, 5, 6}
Process finished with exit code 0
clear() 清空集合
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 从集合中移除所有元素
a.clear()
print(a)
# 运行结果
set()
Process finished with exit code 0
copy() 浅复制
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 返回原集合的浅拷贝
c = a.copy()
print(c)
# 运行结果
{1, 2, 3, 4, 5, 6}
Process finished with exit code 0
update() 更新
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 更新集合,添加来自 b 中的所有元素
a.update(b)
print(a)
# 运行结果
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Process finished with exit code 0
项目中使用较少的方法
isdisjoint()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 如果集合中没有与 b 共有的元素则返回 True。当且仅当两个集合的交集为空集合时,两者为不相交集合
c = a.isdisjoint(b)
print(c)
# 运行结果
False
Process finished with exit code 0
issubset()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 检测是否集合中的每个元素都在 b 之中
c = a.issubset(b)
print(c)
# 运行结果
False
Process finished with exit code 0
issuperset()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 检测是否 b 中的每个元素都在集合之中
c = a.issuperset(b)
print(c)
# 运行结果
False
Process finished with exit code 0
union()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 返回一个新集合,其中包含来自原集合以及 b 指定的所有集合中的元素
c = a.union(b)
print(c)
# 运行结果
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Process finished with exit code 0
intersection()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 返回一个新集合,其中包含原集合以及 b 指定的所有集合中共有的元素
c = a.intersection(b)
print(c)
# 运行结果
{4, 5, 6}
Process finished with exit code 0
difference()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 返回一个新集合,其中包含原集合中在 b 指定的其他集合中不存在的元素
c = a.difference(b)
print(c)
# 运行结果
{1, 2, 3}
Process finished with exit code 0
symmetric_difference()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 返回一个新集合,其中的元素或属于原集合或属于 b 指定的其他集合,但不能同时属于两者
c = a.symmetric_difference(b)
print(c)
# 运行结果
{1, 2, 3, 7, 8, 9}
Process finished with exit code 0
intersection_update()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 更新集合,只保留其中在所有 b 中也存在的元素
a.intersection_update(b)
print(a)
# 运行结果
{4, 5, 6}
Process finished with exit code 0
difference_update()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 更新集合,移除其中也存在于 b 中的元素。
a.difference_update(b)
print(a)
# 运行结果
{1, 2, 3}
Process finished with exit code 0
symmetric_difference_update()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 更新集合,只保留存在于集合的一方而非共同存在的元素
a.symmetric_difference_update(b)
print(a)
# 运行结果
{1, 2, 3, 7, 8, 9}
Process finished with exit code 0
discard()
a = {1, 2, 3, 4, 5, 6}
b = {4, 5, 6, 7, 8, 9}
# 如果元素 1 存在于集合中则将其移除。不存在就返回原集合
a.discard(1)
print(a)
# 运行结果
{2, 3, 4, 5, 6}
Process finished with exit code 0
以上就是集合提供的方法汇总,希望你实践下,能更好的帮助你理解…
以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;非常期待你的点赞和分享哟,谢谢!
未完,待续…
一直都在努力,希望您也是!
转载请注明:XAMPP中文组官网 » python基础知识-集合[set]方法操作