python二级--一篇彻底搞懂set集合

一、特性

集合(set)是一个无序的不重复元素序列。可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

二、set的操作

图.png

1、创建
①使用大括号 { }
s1 = {'hello'}       
print('s1 = ', s1)   
s2 = {1, 2, 3, 4, 1} 
print('s2 = ', s2)   
# s3 = {[1], [2], [1]
# print('s3 = ', s3) 
s4 = {(1, 2), (2), (3
print('s4 = ', s4)   
# s5 = {{'name': 'tom
# print("s5 = ", s5) 

输出结果:

s1 =  {'hello'}
s2 =  {1, 2, 3, 4}
s4 =  {(1, 2), 2, 3}

使用大括号 { }, 不 能 是 列 表 和 字 典 类 型 , 可 以 是 字 符 串 , 数 字 和 元 组 , 可 以 用 逗 号 隔 开 \color{red}{不能是列表和字典类型,可以是字符串,数字和元组,可以用逗号隔开}

②set(value)创建, v a l u e 是 i t e r a b l e 的 对 象 \color{red}{value是iterable的对象} valueiterable
# 字符串                                                      
s1 = set('hello')                                          
# 数字                                                       
s2 = set(12341)                                            
# 列表                                                       
s3 = set([1, 2, 3, 4, 1])                                  
# 元组                                                       
s4 = set((1, 2, 3, 4, 1))                                  
# 字典                                                       
s5 = set({'name': 'tom', 'age': '12', 'nickname': 'tom'})  
print('s1 = ', s1)   
# print('s2 = ', s2) 
print('s3 = ', s3)   
print('s4 = ', s4)   
print('s5 = ', s5)   

输出结果:

s1 =  {'h', 'l', 'e', 'o'}
s3 =  {1, 2, 3, 4}
s4 =  {1, 2, 3, 4}
s5 =  {'age', 'name', 'nickname'}

value不能是数字, T y p e E r r o r : ′ i n t ′ ∗ ∗ o b j e c t ∗ ∗ i s ∗ ∗ n o t ∗ ∗ i t e r a b l e ; 注 意 : 只 能 传 一 个 参 数 \color{red}{TypeError: 'int' ** object ** is **not **iterable;注意:只能传一个参数} TypeError:intobjectisnotiterable

2、添加元素

I、语法格式如下:

s.add( x )

II、将元素 x 添加到集合 s 中,如果元素已存在,则不进行任何操作。

s = set(('tom', 'john', 'marry'))  
print('s = ', s)                   
s.add('ming')                      
print('s = ', s)                   
s.add('tom')                       
print('s = ', s)                   

打印结果:

s =  {'john', 'marry', 'tom'}
s =  {'john', 'marry', 'ming', 'tom'}
s =  {'john', 'marry', 'ming', 'tom'}
3、移除元素

I、语法格式如下:

s.remove( x )

将元素 x 从集合 s 中移除,如果元素不存在,则会发生错误。

s = set(('tom', 'john', 'marry'))  
print('s = ', s)                   
s.remove('tom')                    
print('s = ', s)                   
s.remove('tom')                    
print('s = ', s)                   

打印结果:

s =  {'marry', 'tom', 'john'}
Traceback (most recent call last):
s =  {'marry', 'john'}
  File "/工作/pyDemo/src/web/setDemo.py", line 32, in <module>
    s.remove('tom')
KeyError: 'tom'

此外还有一个方法也是移除集合中的元素,且如果元素不存在,不会发生错误。格式如下所示:

s.discard( x )
s = set(('tom', 'john', 'marry')) 
print('s = ', s)                  
s.discard('tom')                  
print('s = ', s)                  
s.discard('tom')                  
print('s = ', s)                  

打印结果:

s =  {'marry', 'john', 'tom'}
s =  {'marry', 'john'}
s =  {'marry', 'john'}

我们也可以设置随机删除集合中的一个元素,语法格式如下:

s.pop() 
s = set(('tom', 'john', 'marry'))   
print('s = ', s)                    
s.pop()                             
print('s = ', s)                    
s.pop()                             
print('s = ', s)                    

打印结果:

s =  {'marry', 'tom', 'john'}
s =  {'tom', 'john'}
s =  {'john'}

三、集合的交集、合集(并集)、差集

图.png

图.png

四、交流

如果你有更好的解法或者有不同的意见,大家可以畅所欲言,共同提高进步!
群主也是小白一枚,零基础学习python,要勇于提问,大家共同探讨,共同学习进步!

1、加入qq群一起交流学习:818757567

图.png

2、关注微信公众号:python大星

图.png

古人学问无遗力,少壮工夫老始成。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值