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

Python 元组与集合详解

元组(Tuple)

什么是元组

元组是Python中不可变的有序序列,用圆括号表示。

创建元组

python

thistuple = ("apple", "banana", "cherry")
print(thistuple)

访问元组元素

python

# 通过索引访问
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])  # 输出: banana# 负索引(从末尾开始)
print(thistuple[-1])  # 输出: cherry# 索引范围切片
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])  # 输出: ('cherry', 'orange', 'kiwi')# 负索引范围
print(thistuple[-4:-1])  # 输出: ('orange', 'kiwi', 'melon')

元组的不可变性

元组创建后不能修改,但可以通过转换间接修改:

python

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)  # 输出: ('apple', 'kiwi', 'cherry')

遍历元组

python

thistuple = ("apple", "banana", "cherry")
for x in thistuple:print(x)

检查元素是否存在

python

thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:print("Yes, 'apple' is in the fruits tuple")

元组操作

python

# 获取长度
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))# 单元素元组(必须加逗号)
thistuple = ("apple",)
print(type(thistuple))  # <class 'tuple'>thistuple = ("apple")
print(type(thistuple))  # <class 'str'># 连接元组
tuple1 = ("a", "b", "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)  # 输出: ('a', 'b', 'c', 1, 2, 3)# 删除整个元组
thistuple = ("apple", "banana", "cherry")
del thistuple

元组构造函数和方法

python

# 使用tuple()构造函数
thistuple = tuple(("apple", "banana", "cherry"))
print(thistuple)# 元组方法
my_tuple = (1, 2, 3, 2, 4, 2)
print(my_tuple.count(2))    # 输出: 3(计算2出现的次数)
print(my_tuple.index(3))    # 输出: 2(查找3的位置)

集合(Set)

什么是集合

集合是无序、不重复元素的集合,用大括号表示。

创建集合

python

thisset = {"apple", "banana", "cherry"}
print(thisset)

访问集合元素

由于集合无序,不能通过索引访问:

python

thisset = {"apple", "banana", "cherry"}# 遍历集合
for x in thisset:print(x)# 检查元素是否存在
print("banana" in thisset)  # 输出: True

添加元素

python

thisset = {"apple", "banana", "cherry"}# 添加单个元素
thisset.add("orange")
print(thisset)# 添加多个元素
thisset.update(["orange", "mango", "grapes"])
print(thisset)

集合操作

python

# 获取集合长度
thisset = {"apple", "banana", "cherry"}
print(len(thisset))# 删除元素
thisset = {"apple", "banana", "cherry"}thisset.remove("banana")    # 如果元素不存在会报错
thisset.discard("banana")   # 如果元素不存在不会报错# pop()删除任意元素
x = thisset.pop()
print(x)
print(thisset)# 清空集合
thisset.clear()
print(thisset)  # 输出: set()# 删除整个集合
del thisset

集合运算

python

set1 = {"a", "b", "c"}
set2 = {1, 2, 3}# 并集
set3 = set1.union(set2)
print(set3)  # 输出: {1, 2, 3, 'a', 'b', 'c'}# 使用update()合并
set1.update(set2)
print(set1)  # 输出: {1, 2, 3, 'a', 'b', 'c'}

集合构造函数

python

thisset = set(("apple", "banana", "cherry"))
print(thisset)

集合方法总结

方法描述
add()添加元素到集合
clear()移除集合所有元素
copy()返回集合的副本
difference()返回两个集合的差集
difference_update()移除同时在两个集合中的元素
discard()删除指定元素
intersection()返回两个集合的交集
intersection_update()移除不在其他指定集合中的元素
isdisjoint()判断两个集合是否无交集
issubset()判断是否为子集
issuperset()判断是否为超集
pop()移除任意元素
remove()移除指定元素
symmetric_difference()返回两个集合的对称差集
symmetric_difference_update()插入两个集合的对称差集
union()返回两个集合的并集
update()用并集更新集合

总结

  • 元组:有序、不可变序列,适合存储不应修改的数据

  • 集合:无序、不重复元素集合,适合成员检测和去重操作

两者都是Python中重要的数据结构,根据具体需求选择使用。元组保证数据完整性,集合提供高效的成员检测和数学集合运算。

http://www.dtcms.com/a/461519.html

相关文章:

  • 微信小程序的页面生命周期 以及onShow的应用场景
  • 微信小程序入门学习教程,从入门到精通,微信小程序核心 API 详解与案例(13)
  • 企业建站系统知识库管理系统方案
  • 购物网站的排版wordpress个人主页
  • 51c视觉~3D~合集7
  • 生鲜买菜商城APP:便捷生活,触手可及的新鲜体验
  • 网站seo去哪个网站找好做化妆品的网站有哪些
  • Java求职面试:从Spring Boot到Kafka的技术探讨
  • ChatGPT Agent深度总结:从“对话工具”到“超级助理”的AI革命
  • shell编程实战
  • 拟定网站建设合同的工作过程记录拍摄微电影公司
  • 厦门 公司网站建设绵阳做网站的公司
  • 【android 驱动开发十一】pinctrl 子系统
  • 【android驱动开发十二】内核子系统大概-进阶
  • vue前端面试题——记录一次面试当中遇到的题(2)
  • 【pyTorch】关于PyTorch的高级索引机制理解
  • c++ bug 函数定义和声明不一致导致出bug
  • 网站建设需求分析文档手机上制作ppt的软件
  • 推广网站怎么做能增加咨询南宁企业官网seo
  • MATLAB的无线传感器网络(WSN)算法仿真
  • k8s opa集成
  • Nginx 负载均衡通用方案
  • 我的世界怎么做神器官方网站dw网站设计与制作
  • ubuntu22.04发布QT程序步骤
  • Spring Boot:分布式事务高阶玩法
  • 做网站开什么端口网址格式
  • 白云区建设局网站建筑工程网教
  • react native android设置邮箱,进行邮件发送
  • Java面试场景:从Spring Boot到Kubernetes的技术问答
  • 从潜在空间到实际应用:Embedding模型架构与训练范式的综合解析