Python 简明教程
Python - Access Tuple Items
Access Tuple Items
访问 Python 元组内值的常见方式是使用编址,我们只需要将我们想要检索的元素的索引指定给方括号 [] 括号。
The most common way to access values within a Python tuple is using indexing, We just need to specify the index of the elements we want to retrieve to the square bracket [] notation.
除了编址之外,Python 还提供了许多其他访问元组项的方法,例如切片、负索引、从元组中提取子元组等。让我们逐个了解一下:
In addition to indexing, Python provides various other ways to access tuple items such as slicing, negative indexing, extracting a subtuple from a tuple etc. Let us go through this one-by-one −
Accessing Tuple Items with Indexing
元组中的每个元素都对应于一个索引。索引从第一个元素的 0 开始,对于每个后续元素增加一。元组中最后一个元素的索引始终为“长度-1”,其中“长度”表示元组中的总项数。要访问元组的元素,我们只需要指定我们要访问/检索的元素的索引,如下所示:
Each element in a tuple corresponds to an index. The index starts from 0 for the first element and increments by one for each subsequent element. Index of the last item in the tuple is always "length-1", where "length" represents the total number of items in the tuple. To access the elements of a tuple we just need to specify the index of the item we need to access/retrieve, as shown below −
tuple[3]
Example
以下是如何使用切片索引访问元组项的基本示例 −
Following is the basic example to access tuple items with slicing index −
tuple1 = ("Rohan", "Physics", 21, 69.75)
tuple2 = (1, 2, 3, 4, 5)
print ("Item at 0th index in tuple1: ", tuple1[0])
print ("Item at index 2 in tuple2: ", tuple2[2])
它将生成如下输出:
It will produce the following output −
Item at 0th index in tuple1: Rohan
Item at index 2 in tuple2: 3
Accessing Tuple Items with Negative Indexing
Python 中的负索引用于从元组的末尾访问元素, -1 表示最后一个元素, -2 表示倒数第二个元素,依此类推。
Negative indexing in Python is used to access elements from the end of a tuple, with -1 referring to the last element, -2 to the second last, and so on.
我们还可以使用负整数表示从元组末尾开始的位置来访问带负索引的元组项。
We can also access tuple items with negative indexing by using negative integers to represent positions from the end of the tuple.
Example
在以下示例中,我们正在使用负索引访问元组项 −
In the following example, we are accessing tuple items with negative indexing −
tup1 = ("a", "b", "c", "d")
tup2 = (25.50, True, -55, 1+2j)
print ("Item at 0th index in tup1: ", tup1[-1])
print ("Item at index 2 in tup2: ", tup2[-3])
我们得到了如下输出 −
We get the output as shown below −
Item at 0th index in tup1: d
Item at index 2 in tup2: True
Accessing Range of Tuple Items with Negative Indexing
通过元组项的范围,我们是指使用切片从元组中访问元素的子集。因此,我们可以使用 Python 中的切片操作来访问带负索引的元组项的范围。
By range of tuple items, we mean accessing a subset of elements from a tuple using slicing. Therefore, we can access a range of tuple items with negative indexing by using the slicing operation in Python.
Example
在下面的示例中,我们正在使用负索引访问元组项的范围 −
In the example below, we are accessing a range of tuple items by using negative indexing −
tup1 = ("a", "b", "c", "d")
tup2 = (1, 2, 3, 4, 5)
print ("Items from index 1 to last in tup1: ", tup1[1:])
print ("Items from index 2 to last in tup2", tup2[2:-1])
它将生成如下输出:
It will produce the following output −
Items from index 1 to last in tup1: ('b', 'c', 'd')
Items from index 2 to last in tup2: (3, 4)
Access Tuple Items with Slice Operator
Python 中的切片运算符用于从元组中获取一个或多个项。我们可以通过指定我们要提取的索引范围来访问带切片运算符的元组项。它使用以下语法 −
The slice operator in Python is used to fetch one or more items from the tuple. We can access tuple items with the slice operator by specifying the range of indices we want to extract. It uses the following syntax −
[start:stop]
其中,
Where,
-
start is the starting index (inclusive).
-
stop is the ending index (exclusive).
Example
在以下示例中,我们正在从 "tuple1" 中检索从索引 1 到最后一个索引的子元组,从 "tuple2" 中检索从索引 0 到 1 的子元组,并检索 "tuple3" 中的所有元素 −
In the following example, we are retrieving subtuple from index 1 to last in "tuple1" and index 0 to 1 in "tuple2", and retrieving all elements in "tuple3" −
tuple1 = ("a", "b", "c", "d")
tuple2 = (25.50, True, -55, 1+2j)
tuple3 = (1, 2, 3, 4, 5)
tuple4 = ("Rohan", "Physics", 21, 69.75)
print ("Items from index 1 to last in tuple1: ", tuple1[1:])
print ("Items from index 0 to 1 in tuple2: ", tuple2[:2])
print ("Items from index 0 to index last in tuple3", tuple3[:])
以下是上面代码的输出: -
Following is the output of the above code −
Items from index 1 to last in tuple1: ('b', 'c', 'd')
Items from index 0 to 1 in tuple2: (25.5, True)
Items from index 0 to index last in tuple3 ('Rohan', 'Physics', 21, 69.75)
Accessing Sub Tuple from a Tuple
子元组是元组的一部分,由来自原始元组的连续元素序列组成。
A subtuple is a part of a tuple that consists of a consecutive sequence of elements from the original tuple.
我们可以使用带有适当开始和结束索引的切片运算符从元组中访问子元组。它使用以下语法 −
We can access a subtuple from a tuple by using the slice operator with appropriate start and stop indices. It uses the following syntax −
my_tuple[start:stop]
其中,
Where,
-
start is the starting index (inclusive).
-
stop is the ending index (exclusive) of the subtuple.
如果我们不提供任何索引,切片运算符默认从索引 0 开始,在元组的最后一个项处停止。
If we does not provide any indices, the slice operator defaults to starting from index 0 and stopping at the last item in the tuple.
Example
在此示例中,我们正在使用切片运算符从 "tuple1" 中检索索引 "1 到 2" 的子元组,从 "tuple2" 中检索索引 "0 到 1" 的子元组 −
In this example, we are fetching subtuple from index "1 to 2" in "tuple1" and index "0 to 1" in "tuple2" using slice operator −
tuple1 = ("a", "b", "c", "d")
tuple2 = (25.50, True, -55, 1+2j)
print ("Items from index 1 to 2 in tuple1: ", tuple1[1:3])
print ("Items from index 0 to 1 in tuple2: ", tuple2[0:2])
获得的输出如下 −
The output obtained is as follows −
Items from index 1 to 2 in tuple1: ('b', 'c')
Items from index 0 to 1 in tuple2: (25.5, True)