侧边栏壁纸
博主头像
LittleAO的学习小站 博主等级

在知识的沙漠寻找绿洲

  • 累计撰写 125 篇文章
  • 累计创建 27 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Python的Tuple类型详解

LittleAO
2023-04-04 / 0 评论 / 0 点赞 / 9 阅读 / 0 字
温馨提示:
本文最后更新于2023-11-13,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

资料来源于Github项目**30-Days-Of-Python,本文为第六章翻译。**

元组(Tuple)

元组是一种由不同数据类型组成的有序且不可更改(不可变)的集合。元组使用圆括号 () 表示。一旦创建了一个元组,我们就不能更改其值。因为元组是不可修改的,所以我们不能使用添加、插入或删除方法。与列表不同,元组只有少数几个方法:

  • tuple():创建一个空元组。
  • count():计算元组中特定项出现的次数。
  • index():查找元组中特定项的索引位置。
  • 运算符:将两个或多个元组连接起来,创建一个新的元组。

创建Tuple

  • 创建一个空元组
# 语法
empty_tuple = ()
# 或者使用元组构造函数来创建元组。
empty_tuple = tuple()

  • 带有初始值的元组
# 语法
tpl = ('item1', 'item2','item3')

fruits = ('banana', 'orange', 'mango', 'lemon')

Tuple的长度

我们可以使用 len() 方法来获取一个元组的长度。

# 语法
tpl = ('item1', 'item2', 'item3')
len(tpl)

访问Tuple

正向索引与List类型类似,我们使用正向或反向索引来访问元组的项。

# 语法
tpl = ('item1', 'item2', 'item3')
first_item = tpl[0]
second_item = tpl[1]

fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[0]
second_fruit = fruits[1]
last_index =len(fruits) - 1
last_fruit = fruits[las_index]

  • 负索引意味着从最后开始,-1 表示最后一项,-2 表示倒数第二项,而List/Tuple长度的负值表示第一项。
# 语法
tpl = ('item1', 'item2', 'item3','item4')
first_item = tpl[-4]
second_item = tpl[-3]

fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[-4]
second_fruit = fruits[-3]
last_fruit = fruits[-1]

Tuple切片

我们可以通过指定一个起始和结束索引范围来切出一个子元组,返回值将是一个包含指定项的新元组。

  • 正向索引范围
# 语法
tpl = ('item1', 'item2', 'item3','item4')
all_items = tpl[0:4]         # 所有项
all_items = tpl[0:]          # 所有项
middle_two_items = tpl[1:3]  # 没有编号为3的项

fruits = ('banana', 'orange', 'mango', 'lemon')
all_fruits = fruits[0:4]    # 所有项
all_fruits= fruits[0:]      # 所有项
orange_mango = fruits[1:3]  # 没有编号为3的项
orange_to_the_rest = fruits[1:]

  • 负向索引范围
# 语法
tpl = ('item1', 'item2', 'item3','item4')
all_items = tpl[-4:]         # 所有项
middle_two_items = tpl[-3:-1]  # 没有编号为3(-1)的项

fruits = ('banana', 'orange', 'mango', 'lemon')
all_fruits = fruits[-4:]    # 所有项
orange_mango = fruits[-3:-1]  # 没有编号为3(-1)的项
orange_to_the_rest = fruits[-3:]

Tuple转换为List

我们可以将Tuple转换为List,也可以将List转换为Tuple。如果要修改一个Tuple,则需要先将其转换为List

# 语法
tpl = ('item1', 'item2', 'item3','item4')
lst = list(tpl)

fruits = ('banana', 'orange', 'mango', 'lemon')
fruits = list(fruits)
fruits[0] = 'apple'
print(fruits)     # ['apple', 'orange', 'mango', 'lemon']
fruits = tuple(fruits)
print(fruits)     # ('apple', 'orange', 'mango', 'lemon')

检查Tuple中的项

我们可以使用 in 来检查一个项是否存在于Tuple中,它会返回一个布尔值。

# 语法
tpl = ('item1', 'item2', 'item3','item4')
'item2' in tpl # True

fruits = ('banana', 'orange', 'mango', 'lemon')
print('orange' in fruits) # True
print('apple' in fruits) # False
fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment

连接Tuple

我们可以使用加号操作符 + 来连接两个或多个Tuple

# 语法
tpl1 = ('item1', 'item2', 'item3')
tpl2 = ('item4', 'item5','item6')
tpl3 = tpl1 + tpl2

fruits = ('banana', 'orange', 'mango', 'lemon')
vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot')
fruits_and_vegetables = fruits + vegetables

删除Tuple

无法删除Tuple中的单个项,但可以使用 del 删除整个Tuple

# 语法
tpl1 = ('item1', 'item2', 'item3')
del tpl1

fruits = ('banana', 'orange', 'mango', 'lemon')
del fruits

0

评论区