Python起步——集合
2018-10-02 本文已影响0人
leo_shun
1. 集合概念(set)
- 集合是高中数学一个概念
- 一堆确定无序的不重复的数据
- 集合内数据无序,无法使用索引和分片
- 集合内数据不重复
- 集合内数据只能放置可哈希的数据(集合本身不可哈希)
2. 集合定义
# set()函数定义
>>> s = set()
>>> type(s)
<class 'set'>
# 使用大括号定义,内部必须有值,否则为字典
>>> s = {1,3,4}
>>> type(s)
<class 'set'>
>>> s = {}
>>> type(s)
<class 'dict'>
>>> s = {1,2,3,4,43,2,56,33,34,33}
>>> s
{1, 2, 3, 4, 33, 34, 43, 56}
>>> ss = {i for i in s}
>>> ss
{1, 2, 3, 4, 34, 33, 43, 56}
>>> sss = {i for i in s if i % 2 == 0}
>>> sss
{56, 2, 4, 34}
>>> len(s)
8
3. 集合操作
>>> s = {3,4,5,'hello', 'wor', "hell"}
>>> type(s)
<class 'set'>
>>> s
{3, 4, 5, 'hell', 'hello', 'wor'}
>>> 3 in s
True
>>> 'wor'
'wor'
>>> 'wor' in s
True
>>> 34 in s
False
# 向集合添加元素
>>> s = {1,2,3,4}
>>> s.add(5)
>>> s
{1, 2, 3, 4, 5}
# 集合清除
>>> id(s)
1669984368456
>>> s.clear
<built-in method clear of set object at 0x00000184D2D33748>
>>> s.clear()
>>> id(s)
1669984368456
>>> s
set()
# remove 和discard
>>> s = {23,43,43,32, 17, 1, 4, 5}
>>> s
{32, 1, 4, 5, 43, 17, 23}
>>> s.remove(1)
>>> s
{32, 4, 5, 43, 17, 23}
>>> s.discard(4)
>>> s
{32, 5, 43, 17, 23}
>>> s.remove(123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 123
>>> s.discard(123)
>>> s
{32, 5, 43, 17, 23}
# pop 随机移除
>>> s.pop()
32
>>> s
{5, 43, 17, 23}
# intersection: 交集
# difference:差集
# union:并集
# issubset: 检查是否为另一个的子集
# issuperset:检查是否为另一个的超集
>>> s1 = {1,2,3,4,5,6}
>>> s2 = {5,6,7,8,9}
>>> s_1 = s1.intersection(s2)
>>> s_1
{5, 6}
>>> s_2 = s1.difference(s2)
>>> s_2
{1, 2, 3, 4}
>>> s_3 = s1.issubset(s2)
>>> s_3
False
# frozen set: 冰冻集合
>>> s = frozenset()
>>> type(s)
<class 'frozenset'>