Python 简明教程
Python - Access List Items
Access List Items
In Python, a list is a sequence of elements or objects, i.e. an ordered collection of objects. Similar to arrays, each element in a list corresponds to an index.
若要访问列表中的值,我们需要使用方括号 "[]" 表示法,并指定我们要检索的元素的索引。
To access the values within a list, we need to use the square brackets "[]" notation and, specify the index of the elements we want to retrieve.
除此之外,Python 还提供了访问列表项的多种其他方式,例如切片、负索引、从列表中提取子列表等。让我们一个个研究一下 −
In addition to this, Python provides various other ways to access list items such as slicing, negative indexing, extracting a sublist from a list etc. Let us go through this one-by-one −
Accessing List Items with Indexing
如上所述,要使用索引访问列表中的项目,只需用方括号 ("[]") 指定元素的索引,如下所示 −
As discussed above to access the items in a list using indexing, just specify the index of the element with in the square brackets ("[]") as shown below −
mylist[4]
Example
以下是访问列表项的基本示例 −
Following is the basic example to access list items −
list1 = ["Rohan", "Physics", 21, 69.75]
list2 = [1, 2, 3, 4, 5]
print ("Item at 0th index in list1: ", list1[0])
print ("Item at index 2 in list2: ", list2[2])
它将生成如下输出:
It will produce the following output −
Item at 0th index in list1: Rohan
Item at index 2 in list2: 3
Access List Items with Negative Indexing
Python 中的负索引用于从列表的末尾访问元素,其中 -1 指末尾元素, -2 指倒数第二个,依此类推。
Negative indexing in Python is used to access elements from the end of a list, with -1 referring to the last element, -2 to the second last, and so on.
我们还可以使用负整数从列表末尾表示位置,通过负索引访问列表项。
We can also access list items with negative indexing by using negative integers to represent positions from the end of the list.
Example
在以下示例中,我们使用负索引访问列表项 −
In the following example, we are accessing list items with negative indexing −
list1 = ["a", "b", "c", "d"]
list2 = [25.50, True, -55, 1+2j]
print ("Item at 0th index in list1: ", list1[-1])
print ("Item at index 2 in list2: ", list2[-3])
我们得到了如下输出 −
We get the output as shown below −
Item at 0th index in list1: d
Item at index 2 in list2: True
Access List Items with Slice Operator
Python 中的切片运算符用于从列表中获取一个或多个项。我们可以通过指定要提取的索引范围,使用切片运算符访问列表项。它使用以下语法 −
The slice operator in Python is used to fetch one or more items from the list. We can access list 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).
如果我们不提供任何索引,切片运算符默认为从索引 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 list.
Example
在以下示例中,我们从 "list1" 中检索从索引 1 到最后一个索引的子列表,从 "list2" 中检索从索引 0 到索引 1 的子列表,并检索 "list3" 中的所有元素 −
In the following example, we are retrieving sublist from index 1 to last in "list1" and index 0 to 1 in "list2", and retrieving all elements in "list3" −
list1 = ["a", "b", "c", "d"]
list2 = [25.50, True, -55, 1+2j]
list3 = ["Rohan", "Physics", 21, 69.75]
print ("Items from index 1 to last in list1: ", list1[1:])
print ("Items from index 0 to 1 in list2: ", list2[:2])
print ("Items from index 0 to index last in list3", list3[:])
以下是上面代码的输出: -
Following is the output of the above code −
Items from index 1 to last in list1: ['b', 'c', 'd']
Items from index 0 to 1 in list2: [25.5, True]
Items from index 0 to index last in list3 ['Rohan', 'Physics', 21, 69.75]
Access Sub List from a List
子列表是列表的一部分,由原始列表中的连续元素序列组成。我们可以使用切片运算符和适当的起始和停止索引,从列表中访问子列表。
A sublist is a part of a list that consists of a consecutive sequence of elements from the original list. We can access a sublist from a list by using the slice operator with appropriate start and stop indices.
Example
在此示例中,我们使用切片运算符从 "list1" 中检索从索引 "1 到 2" 的子列表,从 "list2" 中检索从索引 "0 到 1" 的子列表 −
In this example, we are fetching sublist from index "1 to 2" in "list1" and index "0 to 1" in "list2" using slice operator −
list1 = ["a", "b", "c", "d"]
list2 = [25.50, True, -55, 1+2j]
print ("Items from index 1 to 2 in list1: ", list1[1:3])
print ("Items from index 0 to 1 in list2: ", list2[0:2])
获得的输出如下 −
The output obtained is as follows −
Items from index 1 to 2 in list1: ['b', 'c']
Items from index 0 to 1 in list2: [25.5, True]