Python12:集合

集合,就是一组去重的元素。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8}

 

Process finished with exit code 0

解释:

设置list_1为集合,差打印。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
print(list_1,list_2)
print(list_1.intersection(list_2)) #print(list_1 & list_2)


print(list_1.union(list_2))  #print(list_1 | list_2)


print(list_1.difference(list_2)) # print(list_1 - list_2)

 

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8} {0, 2, 66, 4, 6, 8, 22}

{8, 2, 4, 6}

{0, 1, 2, 3, 4, 5, 6, 7, 8, 66, 22}

{1, 3, 5, 7}

 

Process finished with exit code 0

解释:“#”号后边的是等价操作。

list_1.intersection(list_2):两个集合的交集。

list_1.union(list_2):两个集合的并集。

list_1.difference(list_2):两个集合的差集。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
print(list_1.issubset(list_2)) # print(list_1 < list_2)

print(list_1.issuperset(list_2)) # print(list_1 > list_2)

 

输出:

False

False

 

Process finished with exit code 0

解释:“#”号后边的是等价操作。

list_1.issubset(list_2)判断list_1是否是list_2的子集。

list_1.issuperset(list_2):判断list_1是否是list_2的父集。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4]) 
print(list_1.symmetric_difference(list_2)) # print(list_1 ^ list_2)

输出:

{0, 1, 66, 7, 3, 22, 5}

 

Process finished with exit code 0

解释:“#”号后边的是等价操作。

list_1.symmetric_difference(list_2)两个集合的并集,去掉交集部分。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
print(list_2.isdisjoint(list_1))

 

输出:

False

 

Process finished with exit code 0

解释:

list_2.isdisjoint(list_1):如果两个集合没有交集,就返回True

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.add(123)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8, 123}

 

Process finished with exit code 0

解释:

list_1.add(123):增加集合元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.update([12,23,34])
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8, 34, 12, 23}

 

Process finished with exit code 0

解释:

list_1.update([12,23,34]):也是增加集合的元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.remove(6)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 7, 8}

 

Process finished with exit code 0

解释:

list_1.remove(6):删除集合中的元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.pop()
print(list_1)

 

输出:

{2, 3, 4, 5, 6, 7, 8}

 

Process finished with exit code 0

解释:

list_1.pop():删除集合中的元素,默认删除开头。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.discard(123)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8}

 

Process finished with exit code 0

解释:

list_1.discard(123):删除集合中的元素,同remove()不同的是,如果集合中没有这个元素,也不会报错,而remove则会报错。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
n = len(list_1)
print(n)

 

输出:

8

 

Process finished with exit code 0

解释:

n = len(list_1):集合的长度。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
print(2 in list_1)

 

输出:

True

 

Process finished with exit code 0

解释:

print(2 in list_1)判断是否在集合中。

posted @   mclind  阅读(173)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示