python中集合set用法手册

本文介绍了Python中集合set的常用操作,包括创建、添加元素、删除元素、集合运算等。详细讲解了add、clear、copy、pop、remove、discard等方法,以及交集、并集、差集和补集的相关函数。同时提到了frozenset用于创建不可变集合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.集合的创建

# 第一种方式创建 set 类型

>>> print(type(set1), set1)

<class 'set'> {1, 3, 6, 'z', 'a', 'b'}



# 第二种方式创建 set 类型

>>> set2 = set(['z', 'a', 'b', 3, 6, 1])

>>> print(type(set2), set2)

<class 'set'> {1, 3, 6, 'z', 'a', 'b'}



# 第三种方式创建 set 类型

>>> set3 = set('hello')

>>> print(type(set3), set3)

<class 'set'> {'o', 'e', 'l', 'h'}

 

2. set 工厂函数

(1)add(self, *args, **kwargs)

  新增一个元素到集合

set1 = {'a', 'z', 'b', 4, 6, 1}

set1.add(8)

set1.add('hello')

print(set1)



# 执行结果:

# {'b', 1, 'a', 4, 6, 8, 'hello', 'z'}

(2) clear()
  清空所有集合元素

set1 = {'a', 'z', 'b', 4, 6, 1}
set1.clear()
print(set1)# 执行结果:# set()

 

(3)copy()
    拷贝整个集合并赋值给变量

set1 = {'a', 'z', 'b', 4, 6, 1}

set2 =set1.copy()

print(set2)



# 执行结果:

# {1, 'a', 4, 6, 'b', 'z'}

 

(4)pop()
    随机删除集合中一个元素,可以通过变量来获取删除的元素

set1 = {'a', 'z', 'b', 4, 6, 1}

ys = set1.pop()

print('set1集合:', set1)

print('删除的元素:', ys)



# 执行结果:

# set1集合: {4, 6, 'z', 'a', 'b'}

# 删除的元素: 1

 

(5)remove(self, *args, **kwargs)
    删除集合中指定的元素,如果该集合内没有该元素就报错

set1 = {'a', 'z', 'b', 4, 6, 1}

set1.remove('a')

print(set1)

set1.remove('x')

print(set1)



# 执行结果:

# {1, 4, 6, 'b', 'z'}

# Traceback (most recent call last):

#   File "D:/learn_python/learn_python/day13/s1.py", line 43, in <module>

#     set1.remove('x')

# KeyError: 'x'

 

(6)discard(self, *args, **kwargs)
    删除集合中指定的元素,如果该集合内没有该元素也不会报错

set1 = {'a', 'z', 'b', 4, 6, 1}

set1.discard('a')

print(set1)

set1.discard('y')

print(set1)



# 执行结果:

# {1, 4, 6, 'b', 'z'}

# {1, 4, 6, 'b', 'z'}

      pop() 、remove() 、 discard() 三个集合删除函数比较:

         pop() 随机删除集合中一个元素

        remove() 删除集合中指定的元素,如果集合中没有指定的元素,程序报错!

        discard() 删除集合中指定的元素,如果集合中没有指定的元素,程序正常运行。

 

(7) intersection  & :交集; union | :并集合; difference - : 差集

set1 = {'a', 'b', 'x', 'y'}

set2 = {'i', 'j', 'b', 'a'}



# 交集

print(set1 & set2)

print(set1.intersection(set2))



# 执行结果:

# {'a', 'b'}

# {'a', 'b'}





# 并集

print(set1 | set2)

print(set1.union(set2))



# 执行结果:

# {'y', 'j', 'a', 'b', 'x', 'i'}

# {'y', 'j', 'a', 'b', 'x', 'i'}



# 差集

print(set1 - set2)

print(set1.difference(set2))

print(set2 - set1)

print(set2.difference(set1))



# 执行结果:

# {'y', 'x'}

# {'y', 'x'}

# {'j', 'i'}

# {'j', 'i'}

 

(8)difference_update ()
    求差集,并赋值给源集合

set1 = {'a', 'b', 'x', 'y'}

set2 = {'i', 'j', 'b', 'a'}

set1.difference_update(set2)

print(set1)



# 执行结果:

# {'y', 'x'}

 

(9)intersection_update()
    求交集,并赋值给源集合

set1 = {'a', 'b', 'x', 'y'}

set2 = {'i', 'j', 'b', 'a'}



set1.intersection_update(set2)

print(set1)



# 执行结果:

# {'b', 'a'}

 

(10)symmetric_difference()  和 ^ 符号效果一样
    求交叉补集

set1 = {'a', 'b', 'x', 'y'}

set2 = {'i', 'j', 'b', 'a'}



print('symmetric_difference:', set1.symmetric_difference(set2))

print('^:', set1 ^ set2)



# 执行结果:

# symmetric_difference: {'x', 'i', 'y', 'j'}

# ^: {'x', 'i', 'y', 'j'}

 

(11)symmetric_difference_update()
  求交叉补集并赋值给源集合

set1 = {'a', 'b', 'x', 'y'}

set2 = {'i', 'j', 'b', 'a'}



set1.symmetric_difference_update(set2)

print(set1)



# 执行结果:

# {'y', 'i', 'j', 'x'

 

(12)update()
    更新集合,参数为可迭代对象

set1 = {'a', 'b', 'x', 'y'}



set1.update(('hello', 'world'))

print(set1)



# 执行结果:

# {'hello', 'world', 'b', 'a', 'y', 'x'}

 

   add() 和 update() 比较:
    add(): 只能添加一个元素到集合
    update(): 可以添加多个元素到集合,参数为 iterable

 

使用 frozenset 定义不可变集合

s = frozenset('hello')

print(s)



# 执行结果:

# frozenset({'h', 'e', 'o', 'l'})

  使用 frozenset 定义的集合,没有 add 或者 pop 等方法

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值