Loading

Python 元组

7.Python 元组

7.1 创建元组

元组(Tuple)

元组是有序且不可更改的集合。在 Python 中,元组是用圆括号编写的。

实例

创建元组:

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

运行结果:

('apple', 'banana', 'cherry')

也可以用tuple() 构造函数创建元组

thistuple = tuple(("apple", "banana", "cherry")) # 请注意双括号
print(thistuple)

7.2 访问元组项目

7.2.1 索引

可以通过引用方括号内的索引号来访问元组项目:

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

运行结果:

banana

负索引

负索引表示从末尾开始,-1 表示最后一个项目,-2 表示倒数第二个项目,依此类推。

打印元组的最后一个项目:

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

运行结果:

cherry

7.2.2 切片

可以通过指定范围的起点和终点来指定索引范围。

指定范围后,返回值将是带有指定项目的新元组。

返回第三、第四、第五个项目:

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

运行结果:

('cherry', 'orange', 'kiwi')

注释:搜索将从索引 2(包括)开始,到索引 5(不包括)结束。

请记住,第一项的索引为 0。

负索引范围

如果要从元组的末尾开始搜索,请指定负索引:

此例将返回从索引 -4(包括)到索引 -1(排除)的项目:

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

运行结果:

('orange', 'kiwi', 'melon')

7.3 更改元组值

创建元组后,您将无法更改其值。元组是不可变的,或者也称为恒定的。

但是有一种解决方法。您可以将元组转换为列表,更改列表,然后将列表转换回元组。

实例

把元组转换为列表即可进行更改:

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

print(x)

运行结果:

('apple', 'kiwi', 'cherry')

7.4 遍历元组

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

遍历项目并打印值:

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

运行结果:

apple
banana
cherry

7.5 检查项目是否存在

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

检查元组中是否存在 "apple":

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

运行结果:

Yes, 'apple' is in the fruits tuple

7.6 元组长度

要确定元组有多少项,请使用 len() 方法:

打印元组中的项目数量:

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

运行结果:

3

7.7 添加项目

元组一旦创建,您就无法向其添加项目。元组是不可改变的。

您无法向元组添加项目:

thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # 会引发错误
print(thistuple)

运行结果:

Traceback (most recent call last):
  File "demo_tuple_add.py", line 2, in <module>
    thistuple[3] = "orange" # This will raise an error
TypeError: 'tuple' object does not support item assignment

7.8 创建有一个项目的元组

如需创建仅包含一个项目的元组,您必须在该项目后添加一个逗号,否则 Python 无法将变量识别为元组。

单项元组,别忘了逗号:

thistuple = ("apple",)
print(type(thistuple))

#不是元组
thistuple = ("apple")
print(type(thistuple))

运行结果:

<class 'tuple'>
<class 'str'>

7.9 删除项目

注释:您无法删除元组中的项目。

元组是不可更改的,因此您无法从中删除项目,但您可以完全删除元组:

del 关键字可以完全删除元组:

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

print(thistuple) # 这会引发错误,因为元组已不存在。

运行结果:

Traceback (most recent call last):
  File "demo_tuple_del.py", line 3, in <module>
    print(thistuple) #this will raise an error because the tuple no longer exists
NameError: name 'thistuple' is not defined

7.10 合并两个元组

如需连接两个或多个元组,您可以使用 + 运算符:

合并这个元组:

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

tuple3 = tuple1 + tuple2
print(tuple3)

运行结果:

('a', 'b', 'c', 1, 2, 3)

7.11 元组方法

Python 提供两个可以在元组上使用的内建方法。

方法 描述
count() 返回元组中指定值出现的次数。
index() 在元组中搜索指定的值并返回它被找到的位置。
posted @ 2022-07-16 09:00  minqiliang  阅读(99)  评论(0编辑  收藏  举报
-->