python 集合类型

There are two types of set:variable set and immutable set:variable set can not be hashed.Note:frozenset can not be changed!

1.create and update

> myset=set("abadsfljasd")
> myset
set(['a', 'b', 'd', 'f', 'j', 'l', 's'])

Note:we can not create a set by operator(can not treated as list ant tuple or diretoty).we can only use set() anf frozenzet().


1.1visit the value in a set:

'a' in myset


1.2update a set

myset.add('z')
myset.update("python")
myset.remove("abcd")
s-=set("python")

1.3delete a set

del myset

2.the oprator of set

2.1 standard operator

in,no in,==,!= , > , >=, <, <=

2.2 'set' type operator

combine: "|"
> twoset=se("xyz")
> oneset | twoset
set(['a', 'c', 'b', 'd', 'y', 'x', 'z'])
&, -, ^
>oneset ^ threeset
>set(['c', 'd', '1', '3', '2'])
mixed-type operate:set and frozen set
> myset=set("abcd")
> myfrozenset=frozenset("1234")
> myset | myfrozenset
set(['a', 'c', 'b', 'd', '1', '3', '2', '4'])
> myfrozenset | myset
frozenset(['a', 'c', 'b', 'd', '1', '3', '2', '4'])
Note:the result type follow the former.

2.3set-type operator---only used for set not for frozenset

Update:|=, &=, -=, ^=

3.build_in function

len(), set(), frozenset()

3.1 set-type build_in function

Note:build_in function means standart build_in function that could used for set . set-type build_in function means this function is part of set-type.

function name           return value

s.add('a')              update s
s.clear()               clear s
s.difference(t)         a new set(element in s but not in t)
s.union(t)              a new set
s.intersection(t)       a new set
s.issubset(t)           True or False

3.2compare: operator and build_in function:

For the most, build_in function and operarot is equal. However, the operand for the operator must be set-type, while the object for the build_in function could be iterative type.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值