当前位置: 首页 > article >正文

【Python】10、集合

文章目录

    • 1. 集合的简介
      • 1.1 集合创建
      • 1.2 集合添加
      • 1.3 集合删除
    • 2. 集合的运算

1. 集合的简介

集合(set)和列表区别如下:

  1. 集合中只能存储不可变对象
  2. 集合中存储的对象是无序的(不是按照元素的插入顺序保存)
  3. 集合中不能出现重复的元素(重复的数据会去重)

1.1 集合创建

  • 使用 {x,x} 来创建集合
s = {1,2,3,4,1,1,1,1,21,2,2,2,15,21,0}

print(s,type(s))

# 结果
{0, 1, 2, 3, 4, 21, 15} <class 'set'>

***Repl Closed***

  • 创建空集合: s = set()
s = set()

print(s,type(s))

# 结果:
set() <class 'set'>

***Repl Closed***

  • 通过set()将序列和字典转化为集合
    使用set()将字典转化为集合时,只会包含字典的键
s = set([1,2,3,2,1,5,4])
s2 = set('hello')
s3 = set({'a':1,'b':3,'c':5})

print(s,type(s))
print(s2,type(s2))
print(s3,type(s3))

# 结果:
{1, 2, 3, 4, 5} <class 'set'>
{'l', 'e', 'o', 'h'} <class 'set'>
{'c', 'a', 'b'} <class 'set'>

***Repl Closed***

集合无法通过索引进行操作

  • 使用 in 和 not in 检查集合中的元素
  • 使用 len() 获取集合中元素个数

1.2 集合添加

  • add() 向集合中添加元素
s = set([1,2,3,2,1,5,4])
s.add(7)

print(s,type(s))

# 结果
{1, 2, 3, 4, 5, 7} <class 'set'>

***Repl Closed***

  • update() 将一个集合中的元素添加到当前集合中
    update()可以传递序列或字典作为参数,字典只会使用键
s = set([1,2,3,2,1,5,4])
s2 = set('world')

s.update(s2)

print(s,type(s))

# 结果:
{1, 2, 3, 4, 5, 'o', 'w', 'r', 'd', 'l'} <class 'set'>

***Repl Closed***

1.3 集合删除

  • pop() 随机删除一个集合中的元素
    pop() 的返回值是被删除的元素
s = {1,2,3,5,4}
print(s)

s.pop()
print(s)

# 结果
{1, 2, 3, 4, 5}
{2, 3, 4, 5}

***Repl Closed***
  • remove(x) 删除集合中指定元素
s = {1,2,3,5,4}
print(s)

s.remove(5)
print(s)

# 结果
{1, 2, 3, 4, 5}
{1, 2, 3, 4}

***Repl Closed***

  • clear() 清空集合
s = {1,2,3,5,4}

s.clear()
print(s)

# 结果
set()

***Repl Closed***

  • copy() 集合的浅复制
s = {1,2,3,5,4}
s2 = s.copy()

print(s,id(s))
print(s2,id(s2))

# 结果
{1, 2, 3, 4, 5} 1734842045088
{1, 2, 3, 4, 5} 1734842044640

***Repl Closed***

2. 集合的运算

对集合运算时,不会影响原来的集合,而是返回一个运算结果

  • & 交集运算
s = {1,2,3,5,4}
s2 = {4,5,7,8,6}

res = s & s2
print(res)

# 结果
{4, 5}

***Repl Closed***

  • | 并集运算
s = {1,2,3,5,4}
s2 = {4,5,7,8,6}

res = s | s2
print(res)

# 结果
{1, 2, 3, 4, 5, 6, 7, 8}

***Repl Closed***

  • - 差集运算
    s - s2,求得是只在s集合中存在的元素
s = {1,2,3,5,4}
s2 = {4,5,7,8,6}

res = s - s2
print(res)

# 结果
{1, 2, 3}

***Repl Closed***

  • ^ 异或集运算
    s ^ s2 得出的是分别只在s 和 s2集合中独立存在的元素,也就是集合不相交的部分
s = {1,2,3,5,4}
s2 = {4,5,7,8,6}

res = s ^ s2
print(res)

# 结果
{1, 2, 3, 6, 7, 8}

***Repl Closed***

  • <= 检查一个集合是否是另一个集合的子集
    如果a集合中的元素全部都在b集合中出现,那么a集合就是b集合的子集,b集合是a集合的超集
s = {1,2,3,5,4}
s2 = {1,5,4}

res = s2 <= s
print(res)

# 结果:
True

***Repl Closed***

  • < 检查一个集合是否是了另一个集合的真子集
    如果超集b中含有子集a中所有元素,并且b中还有a中没有的元素,则b就是a的真超集,a是b的真子集
s = {1,2,3,5,4}
s2 = {1,5,4}

res = s2 < s
print(res)

# 结果
True

***Repl Closed***

  • > 检查一个集合是否是另一个集合的真超集
s = {1,2,3,5,4}
s2 = {1,5,4}

res = s > s2
print(res)

# 结果:
True

***Repl Closed***

  • >= 检查一个集合是否是另一个集合的超集
s = {1,2,3,5,4}
s2 = {1,5,4}

res = s > s2
print(res)

# 结果:
True

***Repl Closed***


http://www.kler.cn/a/590815.html

相关文章:

  • 使用fastapi部署stable diffusion模型
  • 3D点云目标检测——KITTI数据集读取与处理
  • 完全托管的DeepSeek-R1模型正式登陆Amazon Bedrock:安全部署与使用指南
  • Java的继承:方法;属性?
  • 个人学习编程(3-18) leetcode刷题
  • 在云平台上用Claude 3.7 AI代理自动化电脑图形界面点击操作做表格
  • PostgreSQL17允许psql的\watch在返回最小行数后停止
  • 2025年3月19日 十二生肖 今日运势
  • 电子硬件入门(三)——偏置电路
  • 模型评估——acc、P、R、F值、交叉验证、K折交叉验证
  • PATB1113 钱串子的加法
  • C++ 友元 / friend关键字解读
  • MongoDB 只能存储能够序列化的数据(比如字符串、数字等),而 Python 的 UUID 对象并不是直接可以存入数据库的格式。
  • Centos7更换仓库源为阿里云镜像
  • Hyperlane:Rust 生态中的轻量级高性能 HTTP 服务器库,助力现代 Web 开发
  • 力扣题目汇总 使用贪心算法解决问题
  • 热更新解决方案5——toLua
  • TCP 客户端 - 服务器通信程序搭建
  • ora-600 ktugct: corruption detected---惜分飞
  • 晶艺代理,100V3.5A高耐压LA1823完全替换MP9487--启烨科技有限公司