Python3 集合

Posted yujiaershao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3 集合相关的知识,希望对你有一定的参考价值。

    集合相关的功能详解

 

 

    

集合(set)是一个无序的不重复元素序列。

可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

创建格式:

parame = {value01,value02,...}
或者
set(value)


  • add: 添加元素
st = {1, 2, ‘aa‘, ‘bb‘}

st.add(3)

print(st)
输出;
{1, 2, 3, ‘bb‘, ‘aa‘}


  • clear:清空
st = {1, 2, ‘aa‘, ‘bb‘}

st.clear()

print(st)
输出:
set()


  • copy:浅拷贝
st = {1, 2, ‘aa‘, ‘bb‘}

v = st.copy()

print(st)
输出:
{‘bb‘, 1, 2, ‘aa‘}


  • discard:移除,如果元素不存在,不报错
st = {1, 2, ‘aa‘, ‘bb‘}
#st2 = {1, 2, ‘aa‘ ,‘cc‘}

st.discard(‘aa‘)
print(st)
st.discard(‘cc‘)
print(st)
输出:

{1, 2, ‘bb‘}
{1, 2, ‘bb‘}



  • remove:移除,如果元素不存在,报错
st = {1, 2, ‘aa‘, ‘bb‘}
#st2 = {1, 2, ‘aa‘ ,‘cc‘}

st.remove(‘aa‘)
print(st)
st.remove(‘cc‘)
print(st)

输出:

{1, 2, ‘bb‘}
Traceback (most recent call last):
File "C:/Users/PycharmProjects/t3.py", line 8, in <module>
st.remove(‘cc‘)
KeyError: ‘cc‘



  • pop:随机移除
st = {1, 2, ‘aa‘, ‘bb‘}
#st2 = {1, 2, ‘aa‘ ,‘cc‘}

st.pop()
print(st)
输出:

{1, 2, ‘bb‘}



  • update:修改成union之后的元素。即所有元素
st = {3, 4, ‘dd‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘bb‘}

st.update(st2)
print(st)
输出:
{1, 2, 3, 4, ‘aa‘, ‘bb‘, ‘dd‘}


  • difference: 集合的差集,相当于 ‘-‘
st = {1, 2, ‘aa‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘cc‘}
v = st2 - st
print(v)
v = st2.difference(st)
print(v)
输出:

{‘cc‘}

 

 

 

  • difference_update:移除集合中存在另一集合的元素
st = {1, 2, ‘aa‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘cc‘}

st2.difference_update(st)
print(st2)
输出:
{‘cc‘}

 

  • intersection:交集,’&‘
st = {1, 2, ‘aa‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘cc‘}

v = st.intersection(st2)
print(v)
v = st & st2
print(v)
输出:

{‘aa‘, 1, 2}
{‘aa‘, 1, 2}



  • intersection_update:求交集并更改原集合
st = {1, 2, ‘aa‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘cc‘}

st.intersection_update(st2)
print(st)
print(st2)
输出:

{1, 2, ‘aa‘}
{‘cc‘, 1, 2, ‘aa‘}



  • isdisjoint: 是否存在交集,存在返回false,不存在返回True
st = {3, 4, ‘dd‘, ‘bb‘}
st1 = {1, 2, ‘aa‘ ,‘cc‘}
st2 = {1, 2, ‘aa‘ ,‘bb‘}

v = st.isdisjoint(st1)
print(v)
v2 = st.isdisjoint(st2)
print(v2)
输出:

True
False



  • issubset:是否是另外集合的子集,’<‘
st = {3, 4, ‘dd‘, ‘bb‘}
st1 = {1, 2, ‘aa‘ }
st2 = {1, 2, ‘aa‘ ,‘bb‘}

v = st.issubset(st1)
print(v)
v2 = st1.issubset(st2)
print(v2)
v3 = st1 < st2
print(v3)
输出:

False
True
True

 

  • issuperset:是否是另外一个集合的父集,‘>‘
st = {3, 4, ‘dd‘, ‘bb‘}
st1 = {1, 2, ‘aa‘ }
st2 = {1, 2, ‘aa‘ ,‘bb‘}

v = st.issubset(st1)
print(v)
v2 = st1.issubset(st2)
print(v2)
v3 = st1 < st2
print(v3)
输出:
True
True


  • symmetric_difference: 两个集合不同的元素
st = {3, 4, ‘dd‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘bb‘}

v = st.symmetric_difference(st2)
print(v)
输出:
{1, 2, 3, 4, ‘dd‘, ‘aa‘}


  • symmetric_difference_update: 更改集合1为两集合不同的元素
st = {3, 4, ‘dd‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘bb‘}

st.symmetric_difference_update(st2)
print(st)
输出:
{1, 2, 3, 4, ‘dd‘, ‘aa‘}


  • union:查看集合所有元素, ‘|‘
st = {3, 4, ‘dd‘, ‘bb‘}
st2 = {1, 2, ‘aa‘ ,‘bb‘}

v = st.union(st2)
v2 = st | st2
print(v)
print(v2)
输出:

{1, 2, 3, 4, ‘aa‘, ‘dd‘, ‘bb‘}
{1, 2, 3, 4, ‘aa‘, ‘dd‘, ‘bb‘}


 

























































































































































以上是关于Python3 集合的主要内容,如果未能解决你的问题,请参考以下文章

scrapy按顺序启动多个爬虫代码片段(python3)

代码片段 - Golang 实现集合操作

laravel特殊功能代码片段集合

金蝶handler中 collection 代码片段理解

Alfred常见使用

python常用代码片段总结