author:once Day date:2022年2月16日
本文档在于总结相关内容,零散的知识难以记忆学习。
本文档基于windows平台。
全系列文档查看:python基础_CSDN博客。
文章目录Python集合set
1.创建集合2.集合基本操作
2.1 set() 创建集合对象2.2 集合运算(差-,并|,交&,异或^) 3.集合方法和函数
3.1 add() 添加一个元素到集合中3.2 clear() 清空集合所有元素3.3 copy() 浅复制集合3.4 update() 用集合、列表、元组、字符串等添加元素3.5 remove(x) 移除元素x3.6 discard(x) 移除集合中元素3.7 pop()随机删除集合中的一个元素3.8 len() 计算元素长度3.9 in 成员资格运算3.10 difference() 返回多个集合的差集3.11 difference_update() 移除相同的元素3.12 intersection()和intersection_update() 取集合的交集3.13 symmetric_difference() 和symmetric_difference_update() 取集合的异或3.14 union()返回两个集合的并集3.15 issubset() 判断子集3.16 isdisjoint 判断集合是否包含相同元素(是否相交)3.17 issuperset() 判断超集 注:本文章内容收集整理于互联网,仅供学习只用!
1.创建集合集合(set)是一个无序的不重复元素序列。因此在每次运行的时候集合的运行结果的内容都是相同的,但元素的排列顺序却不是固定的,所以运行结果不唯一。
创建集合使用{}和set()即可,里面可以包含其他对像,如数字和字符串,但要求可以哈希散列。
>>> {1,2,3,'set'}
{1, 2, 3, 'set'}
>>> {1,2,3,'set','set1',3}
{1, 2, 3, 'set1', 'set'}#会自动去掉重复的元素
注意创建空集合时不能用{},可以使用set():
>>> a={}
>>> type(a)
>>> b=set()
>>> b
set()
>>> type(b)
2.集合基本操作
2.1 set() 创建集合对象
可以把字符串,列表以及其他可以接受的数据转化为集合:
>>> set([1,2,'jj',[4,'jk']]) #内嵌的列表无法哈希散列计算,所以创建失败 Traceback (most recent call last): File "2.2 集合运算(差-,并|,交&,异或^)", line 1, in TypeError: unhashable type: 'list' >>> set([1,2,'jj','sss']) {1, 2, 'jj', 'sss'} >>> set('hellword!') {'d', 'r', '!', 'e', 'w', 'o', 'h', 'l'}
>>> a=set('hello')
>>> b=set('once')
>>> a-b #在a不在b的元素
{'h', 'l'}
>>> b-a #在b不在a的元素
{'n', 'c'}
>>> a|b #a和b的全部元素
{'c', 'e', 'o', 'h', 'n', 'l'}
>>> a&b #a和b都有的元素
{'o', 'e'}
>>> a^b #a和b中两者单独拥有的元素
{'c', 'h', 'n', 'l'}
3.集合方法和函数
3.1 add() 添加一个元素到集合中
>>> a=set('hello')
>>> a.add('w')
>>> a
{'e', 'w', 'o', 'h', 'l'}
3.2 clear() 清空集合所有元素
>>> a=set('hello word')
>>> a
{'d', 'r', 'e', 'w', 'o', 'h', 'l', ' '}
>>> a.clear()
>>> a
set()
3.3 copy() 浅复制集合
只复制了集合和元素的映射关系,其中对象只有一个!
>>> a=set('hello word')
>>> b=a.copy()
>>> b
{'d', 'r', 'e', 'w', 'o', 'h', 'l', ' '}
3.4 update() 用集合、列表、元组、字符串等添加元素
>>> a=set('hello')
>>> a.update([1,4,2],'once')
>>> a
{1, 2, 4, 'c', 'e', 'o', 'h', 'n', 'l'}
>>>
3.5 remove(x) 移除元素x
>>> a=set('hello python')
>>> a
{'p', 'e', 'y', 't', 'o', 'h', 'n', 'l', ' '}
>>> a.remove('hello') #没有会发生错误
Traceback (most recent call last):
File "", line 1, in
KeyError: 'hello'
>>> a.remove('h')
>>> a
{'p', 'e', 'y', 't', 'o', 'n', 'l', ' '}
3.6 discard(x) 移除集合中元素
>>> a=set('hello python')
>>> a.discard('h')
>>> a
{'p', 'e', 'y', 't', 'o', 'n', 'l', ' '}
>>> a.discard('hell')#没有的元素不会报错
>>> a
{'p', 'e', 'y', 't', 'o', 'n', 'l', ' '}
3.7 pop()随机删除集合中的一个元素
在python3.9版本的测试结果显示,删除的是第一个元素,然后整体元素前移。
>>> a=set('hello python')
>>> a
{'p', 'e', 'y', 't', 'o', 'h', 'n', 'l', ' '}
>>> a.pop()
'p'
>>> a
{'e', 'y', 't', 'o', 'h', 'n', 'l', ' '}
>>> a.pop()
'e'
>>> a.pop()
'y'
>>> a.pop()
't'
>>> a.pop()
'o'
>>> a.pop()
'h'
>>> a.pop()
'n'
>>> a.pop()
'l'
3.8 len() 计算元素长度
>>> a=set('hello python')
>>> len(a)
9
>>> a
{'p', 'e', 'y', 't', 'o', 'h', 'n', 'l', ' '}
3.9 in 成员资格运算
>>> a=set('hello python')
>>> a
{'p', 'e', 'y', 't', 'o', 'h', 'n', 'l', ' '}
>>> 'p' in a
True
>>> 'p' not in a
False
3.10 difference() 返回多个集合的差集
>>> a=set('hello')
>>> b=set('once')
>>> a.difference(b)
{'h', 'l'}
3.11 difference_update() 移除相同的元素
注意difference不会更改原来集合,而这个在原来集合上更改。
>>> a
{'o', 'h', 'l', 'e'}
>>> b
{'o', 'n', 'c', 'e'}
>>> a.difference_update(b)
>>> a
{'h', 'l'}
>>>
3.12 intersection()和intersection_update() 取集合的交集
注意intersection_update() 会修改原来的集合
>>> a=set('hello')
>>> b=set('once')
>>> a.intersection(b)
{'o', 'e'}
>>> a
{'o', 'h', 'l', 'e'}
>>> a.intersection_update(b)
>>> a
{'o', 'e'}
3.13 symmetric_difference() 和symmetric_difference_update() 取集合的异或
注意symmetric_difference_update()会修改原来的集合
>>> a=set('hello')
>>> b=set('once')
>>> a.symmetric_difference(b)
{'c', 'h', 'n', 'l'}
>>> a
{'o', 'h', 'l', 'e'}
>>> a.symmetric_difference_update(b)
>>> a
{'c', 'h', 'n', 'l'}
3.14 union()返回两个集合的并集
>>> a=set('hello')
>>> b=set('once')
>>> a.union(b)
{'c', 'e', 'o', 'h', 'n', 'l'}
>>> a
{'o', 'h', 'l', 'e'}
3.15 issubset() 判断子集
issubset() 方法用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。
>>> a=set('hello')
>>> b=set('once')
>>> c=set('hello word')
>>> a.issubset(b)
False
>>> a.issubset(c)
True
3.16 isdisjoint 判断集合是否包含相同元素(是否相交)
isdisjoint() 方法用于判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
>>> a=set('hello')
>>> b=set('once')
>>> c=set('day')
>>> a.isdisjoint(b)
False
>>> a.isdisjoint(c)
True
3.17 issuperset() 判断超集
issuperset() 方法用于判断指定集合的所有元素是否都包含在原始的集合中,如果是则返回 True,否则返回 False。
>>> a=set('hello')
>>> b=set('once')
>>> c=set('hello python!')
>>> a.issuperset(b)
False
>>> a.issuperset(c)
False
>>> c.issuperset(a)
True
注:本文章内容收集整理于互联网,仅供学习只用!


