python之元组(tuple)知识点

元组与列表都是容器,两个的区别在于:

1.元组使用的是小括号,列表使用的是方括号

2.元组一旦定义不可修改,而列表是可以随意变更

创建元组

元组的创建与列表大同小异,逗号在元组中充当了元组的灵魂,创建元组时你可以不带上括号,但是必须带上逗号

复制代码
# 创建空元组
tuple1 = tuple()
print(tuple1)
# 创建一个元素的元组,后面必须带上英文逗号(,)
tuple2 = ("fruit",)
print(tuple2)
# 创建元组时不带括号
tuple3 = "fruit", "pear", "apple", "banana"
print(tuple3)

打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
()
('fruit',)
('fruit', 'pear', 'apple', 'banana')
复制代码

通过tuple()方法传入可迭代对象创建元组

复制代码
# 通过tuple()方法将list转为tuple
tuple4 = tuple(["egg", "rice", "bacon"])
print(tuple4)
# 通过tuple()方法传入range对象
tuple5 = tuple(range(10))
print(tuple5)
# 通过tuple()方法传入dict
tuple6 = tuple({"rice": "", "bacon": "培根"})
print(tuple6)
# 通过tuple()方法传入str
tuple7 = tuple("hello")
print(tuple7)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
('egg', 'rice', 'bacon')
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
('rice', 'bacon')
('h', 'e', 'l', 'l', 'o')
复制代码

元组的读取

元组的读取与列表类似,通过下标读取,下标索引从0开始,下标不存在时抛出越界异常

复制代码
# 读取下标为2的元素
tuple_1 = tuple3[2]
print(tuple_1)
# 尝试读取下标不存在的元素
tuple_2 = tuple3[5]
print(tuple_2)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
Traceback (most recent call last):
  File "D:/demo/tuple_1.py", line 15, in <module>
    tuple_2 = tuple3[5]
IndexError: tuple index out of range
apple
复制代码

使用for语句遍历读取

复制代码
for tuple_ in tuple3:
    print(tuple_)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
fruit
pear
apple
banana
复制代码

元组切片

元组也可以进行切片操作,与列表一样,可参考列表切片操作,此处不再赘述

复制代码
print(tuple3)
# 截取整个元组
slice1 = tuple3[:]
# 截取偶数位的所有元素
slice2 = tuple3[::2]
# 反向截取
slice3 = tuple3[::-1]
print(slice1)
print(slice2)
print(slice3)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
('fruit', 'pear', 'apple', 'banana')
('fruit', 'pear', 'apple', 'banana')
('fruit', 'apple')
('banana', 'apple', 'pear', 'fruit')
复制代码

元组删除

复制代码
# 元组中的元素不可编辑,只能删除整个元组
del tuple3
print(tuple3)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
Traceback (most recent call last):
  File "D:/demo/tuple_1.py", line 47, in <module>
    print(tuple3)
NameError: name 'tuple3' is not defined
复制代码

元组中其他常用方法

 count()用于计算某个元素在元组中出现的次数

count_ = tuple3.count("rice")
print(count_)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
0

统计字符出现的个数或元组内出现的元素次数等也可以用 Counter方法, Counter 是一个 dict 的子类,用于计数可哈希对象

from collections import Counter
counter_ = Counter(tuple3)
print(counter_)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
Counter({'fruit': 1, 'pear': 1, 'apple': 1, 'banana': 1})

tuple 中的index()方法同list中的index()方法,用来查找某个元素所在的索引位置,当元组中有多个相同的元素时,默认返回第一个匹配的元素索引位置,若找不到则抛出异常

格式:[obj, start, end]

obj :需要查找的对象

start: 起始查找位置,不填写时默认为0

end: 结束查找位置,不包含end,不填写时默认为元组长度

index_ = tuple3.index("pear", 0, 3)
print(index_)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/tuple_1.py
1

 

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