Python 元组

Python 元组类似于列表,可以存贮多个元素。元组中数据是有序的,可重复的,元组是不可变的。

Python 元组类似于列表,可以存贮多个元素。元组中数据是有序的,可重复的,元组是不可变的。

元组是 Python 中用于存储数据集合的 4 种内置数据类型之一,其他 3 种是列表集合字典

创建 Python 元组可以使用元组字面量(小括号 ())或者 tuple() 构造函数。

mytuple = ("apple", "banana", "cherry")
mytuple = tuple(("apple", "banana", "cherry")

Python 元组具有如下特征:

  • 元组可以使用索引获取其中的元素,和其他语言一样,元组的索引从 0 开始。
  • 元组中的元素按照顺序保存。
  • 元组是不可变的。
  • 元组中元素可以重复存在。
  • 元组中的元素的数据类型可以不同。

元组的类型

Python 元组的数据类型为 <class 'tuple'>

打印元组的数据类型:

mytuple = ("apple", "banana", "cherry")
print(type(mytuple))  # 输出 <class 'tuple'>

访问项目

元组可以通过方括号中的索引号访问其中的项目。

打印元组中的第二项:

thistuple = ("apple", "banana", "cherry")
print(thistuple[1]) # banana

索引也可以使用负数,负索引是指从最后开始,-1 指最后一项, -2 指倒数第二项等。

打印元组的最后一项:

thistuple = ("apple", "banana", "cherry")
print(thistuple[-1]) # cherry

子元组

像列表一样,可以通过切片语法指定开始索引和结束索引(不包含)返回一个子元组,返回的子元组是一个包含了部分元素的新元组。

返回第三、第四和第五项:

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5]) # ('cherry', 'orange', 'kiwi')

省略开始索引,则开始索引为 0; 省略结束索引,则结束索引为最后一个元素的索引。

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[:4]) # ('apple', 'banana', 'cherry', 'orange')

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:]) # ('cherry', 'orange', 'kiwi', 'melon', 'mango')

当索引为负数的时候,是指从最后开始,-1 指最后一项, -2 指倒数第二项等。

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1]) # ('orange', 'kiwi', 'melon')

检查项目是否存在

要确定元组中是否存在指定的项目,请使用 in 关键字。

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

输出: Yes, 'apple' is in the fruits tuple

更新元组

元组是不可更改的,这意味着一旦创建了元组,您就无法更改、添加或删除项目,但是有一些解决方法。

更改元组值

一旦创建了元组,就不能更改其值。元组是不可更改的,是不可变的。但是有一个解决方法。您可以将元组转换为列表,更改列表,然后将列表转换回元组。

将元组转换为列表以便能够更改它:

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x) # ('apple', 'kiwi', 'cherry')

!! 注意:新的 x 元组已经不是以前的元组了,是一个新的元组。

添加项目

由于元组是不可变的,它们没有内置 append() 方法,但有其他方法可以向元组添加项。

  • 转换为列表

    就像更改元组的解决方法一样,您可以将其转换为列表,添加您的项目,然后将其转换回元组。

    thistuple = ("apple", "banana", "cherry")
    y = list(thistuple)
    y.append("orange")
    thistuple = tuple(y)
    
    print(thistuple) #('apple', 'banana', 'cherry', 'orange')
    
  • 连接元组

    您可以向元组添加元组,因此如果您想添加一个(或多个)项目,请使用该项目创建一个新元组,并将其添加到现有元组中:

    thistuple = ("apple", "banana", "cherry")
    y = ("orange",)
    thistuple += y
    
    print(thistuple) # ('apple', 'banana', 'cherry', 'orange')
    

    **注意:**创建只有一个 item 的元组时,记得在 item 后面加上逗号,否则不会被识别为元组。

删除项目

元组是不可更改的,无法从中删除项目,但您可以使用与我们用于更改和添加元组项目相同的解决方法。

将元组转换为列表,删除“apple”,然后将其转换回元组:

thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)

print(thistuple) # ('banana', 'cherry')

解包元组

解包元组是指将元组的项目提取给相应的变量。

解包元组:

fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)  # apple
print(yellow) # banana
print(red)    # cherry

上例中定义了 3 个变量分别赋值为元组中的 3 个元素。

解包元组时,变量的数量必须与元组中值的数量相匹配,否则必须使用星号将剩余的值收集为列表。

如果变量的数量少于元组项目数量,您可以在变量名中添加一个 * ,将多余的元组项目作为列表分配给变量。

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green)  # apple
print(yellow) # banana
print(red)    # ['cherry', 'strawberry', 'raspberry']

如果添加星号的变量不是最后一个,则返回中间多余的元组项目作为列表。

fruits = ("apple", "mango", "papaya", "pineapple", "cherry")

(green, *tropic, red) = fruits

print(green)  # apple
print(tropic) # ['mango', 'papaya', 'pineapple']
print(red)    # cherry

遍历元组

遍历元组元素

使用 for 循环遍历元组项目。

遍历项目并打印值:

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

如有需要,请跳转到 Python For 循环章节中了解有关 for 循环的更多信息。

通过索引遍历元组

使用 range()len() 函数创建包含了索引号的可迭代对象。

通过索引号打印所有项目:

thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
  print(thistuple[i])

使用 While 循环

使用 while 循环遍历列表项。

thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
  print(thistuple[i])
  i = i + 1

如有需要,请跳转到 Python While 循环章节中了解有关 while 循环的 更多信息。

连接元组

在 Python 中有多种方法可以连接或连接两个或多个元组,最简单的方法是使用 + 运算符。

连接两个元组:

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3) # ('a', 'b', 'c', 1, 2, 3)

乘以元组

如果要将元组的内容重复几次,可以使用* 运算符。

fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2

print(mytuple) # ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')

元组方法

Python 有两个内置方法可以用于元组。

方法 说明
count() 返回指定的指在元组中出现的次数
index() 查找指定的指,找到后返回索引位置