组合数据类型(集合)

1,创建集合

a,直接创建

parame = {'a', 'b', 'c', 'd', 'e'} 
print (parame)
>>>{'d', 'a', 'b', 'c', 'e'}

  b,函数创建

parame = set('abcde')                 
print (parame)
>>>{'d', 'a', 'b', 'c', 'e'}

  注:创建空集合时用set()函数,而不是{},因为{}直接创建空字典

2,基本操作

a,判断 

parame = {'a', 'b', 'c', 'd', 'e'}
print ('a' in parame)
>>>True

3,常用功能

a,追加

add()

parame = {'a', 'b', 'c', 'd', 'e'}
parame.add('vitcor')                     # 添加元素是数字、字符串
print (parame)
>>> {'b', 'c', 'd', 'e', 'vitcor', 'a'}

  update()

parame = {'a', 'b', 'c', 'd', 'e'}
parame.upadte('vitcor')               # 将添加的字符串分割掉
print (parame)
>>>{'v', 'b', 'c', 'd', 'i', 'r', 'e', 'a', 'o', 't'} 
parame = {'a', 'b', 'c', 'd', 'e'}
parame.update([1,2],['victor','maomao'])     # 添加元素可以是列表,字典,元组
print (parame)
>>>{1, 2, 'b', 'c', 'd', 'victor', 'e', 'maomao', 'a'}  
parame = {'a', 'b', 'c', 'd', 'e'}
parame.update('a')         
print (parame)
>>>{'d', 'a', 'b', 'c', 'e'}            # 元素存在,不进行任何操作

  b,删除

remove()

parame = {'a', 'b', 'c', 'd', 'e'}
parame.remove('a')          # 若元素不存在,将会报错
print (parame)
>>>{'d', 'b', 'c', 'e'}

discard()

parame = {'a', 'b', 'c', 'd', 'e'}
parame.discard('a')          # 删除特定元素不存在时,不会报错
print (parame)
>>>{'d', 'a', 'b', 'c', 'e'}

c,计数

parame = {'a', 'b', 'c', 'd', 'e'}
x = len(parame)
print (x)
>>>5

d,清空

parame = {'a', 'b', 'c', 'd', 'e'}
parame.clear()
print (parame)
>>>set()

 

转载于:https://www.cnblogs.com/qianqicheng/p/10577281.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值