Python笔记5----集合set

1、集合的概念:无序不重复

分为可变集合(set())和不可变集合(frozenset)两种

2、创建集合

aset=set('hello')

>>aset={'h','e','l','o'}

3、集合的基本运算

数学符号 Python符号 意思
- 或        - 差集
& 交集
| 并集
!= 不等于
== 等于
in 有成员资格
not in 没有成员资格

增:aset.add('world')    >>aset={'h','world','e','o','l'}

更新:aset.update('world')    >>aset={'h','w','r','l','o','d','e'}

删:aset.remove('w')            >>aset={'h','r','l','o','d','e'}

aset={'h','e','l','o'}

bset={'w','o','r','l','d'}

交:aset&bset    >>{'o','l'}

并:aset|bset     >>{'h','e','l','o','w','r','d'}

差:aset-bset    >>{'h','e'}    即aset有的bset没有的

等于:aset==bset   >>False

判断是否是子集:aset.issubset(bset)

转变成list或者tuple: list(aset)      tuple(aset)

原文地址:https://www.cnblogs.com/Lee-yl/p/8624034.html