文章目录
前言
元组(Tuple
)
元组是有序且不可更改的集合。在 Python 中,元组是用圆括号编写的。
如何创建元组:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
一、如何访问元组项目
访问元组项目
我们可以通过引用方括号内的索引号来访问元组项目。
打印元组中的第二个项目:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
负索引
负索引表示从末尾开始,-1 表示最后一个项目,-2 表示倒数第二个项目,依此类推。
打印元组的最后一个项目:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
索引范围
我们可以通过指定范围的起点和终点来指定索引范围。
指定范围后,返回值将是带有指定项目的新元组。
返回第三、第四、第五个项目:
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')
更改元组值
创建元组后,我们将无法更改其值。元组是不可变的
,或者也称为恒定的。
但是有一种解决方法。我们可以将元组转换为列表,更改列表,然后将列表转换回元组
。
把元组转换为列表即可进行更改:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x) # ('apple', 'kiwi', 'cherry')
遍历元组
我们可以使用 for
循环遍历元组项目。
遍历项目并打印值:
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
# apple
# banana
# cherry
检查项目是否存在
要确定元组中是否存在指定的项,请使用 in
关键字:
检查元组中是否存在 “apple”:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple: # True
print("Yes, 'apple' is in the fruits tuple")
元组长度
要确定元组有多少项,请使用 len()
方法:
打印元组中的项目数量:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple)) # 3
添加项目
元组一旦创建,我们就无法向其添加项目。元组是不可改变的
。
我们无法向元组添加项目:
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # 会引发错误 TypeError: 'tuple' object does not support item assignment
print(thistuple)
创建有一个项目的元组
如需创建仅包含一个项目的元组,我们必须在该项目后添加一个逗号,否则 Python 无法将变量识别为元组。
单项元组,别忘了逗号:
thistuple = ("apple",)
print(type(thistuple)) # <class 'tuple'>
#不是元组
thistuple = ("apple")
print(type(thistuple)) # <class 'str'>
删除项目
注意
:我们无法删除元组中的项目。
元组是不可更改的,因此我们无法从中删除项目,但我们可以完全删除元组:
del
关键字可以完全删除元组:
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) # 这会引发错误,因为元组已不存在。
合并两个元组
如需连接两个或多个元组,我们可以使用 +
运算符:
合并这个元组:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3) # ('a', 'b', 'c', 1, 2, 3)
tuple() 构造函数
也可以使用 tuple()
构造函数来创建元组。
使用 tuple()
方法来创建元组:
thistuple = tuple(("apple", "banana", "cherry")) # 请注意双括号
print(thistuple)
二、元组方法
Python 提供两个
可以在元组上使用的内建方法
。
count()
返回元组中指定值出现的次数。
index()
在元组中搜索指定的值并返回它被找到的位置。
总结
元组一旦被创建就无法对其中的内容进行增删改查
。
我们可以利用元组的有序性
来进行集合的去重
list1 = ['ok', 'a', 'ok', 'b', 'c']
new_list = list(set(list1))
print(new_list) # ['a', 'b', 'c', 'ok']