访问列表 (Accessing a list)
To access a list – we can simply print the list object and the complete list prints as an output.
要访问列表,我们可以简单地打印列表对象,然后将完整的列表作为输出打印。
print (list_object)
Example:
例:
In this example, we will declare and assign the list, will print their types, and will print the list. To find the type of an object – we use type() method.
在此示例中,我们将声明并分配列表,打印其类型,并打印列表。 要找到对象的类型–我们使用type()方法 。
# declaring lists
list1 = [10, 20, 30, 40, 50, 10, 60, 10]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]
# printing the list and its elements
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
# printing the types
print("Type of list1 object: ", type(list1))
print("Type of list2 object: ", type(list2))
print("Type of list3 object: ", type(list3))
Output
输出量
list1: [10, 20, 30, 40, 50, 10, 60, 10]
list2: ['Hello', 'IncludeHelp']
list3: ['Hello', 10, 20, 'IncludeHelp']
Type of list1 object: <class 'list'>
Type of list2 object: <class 'list'>
Type of list3 object: <class 'list'>
根据索引访问列表元素 (Accessing list elements based on the index)
To access list elements based on the given index – we simply pass the index starting from 0 to length-1 to access the particular element and we can also pass the negative index to access the list elements in the reverse order (-1 to access the last element, -2 to access the second last element, and so on...)
要基于给定的索引访问列表元素 –我们只需将索引从0开始传递到length-1即可访问特定元素,我们还可以传递负索引以相反的顺序访问列表元素( -1可以访问最后一个元素, -2访问第二个最后一个元素,依此类推...)
Syntax:
句法:
list_object[index]
Example:
例:
# declaring lists
list1 = [10, 20, 30, 40, 50]
# Accessing the elements of a list by its index
print("list1[0]: ", list1[0])
print("list1[1]: ", list1[1])
print("list1[2]: ", list1[2])
print("list1[3]: ", list1[3])
print("list1[4]: ", list1[4])
print() # prints a new line
# Accessing the elements of a list by its index
# in reverse order
print("list1[-1]: ", list1[-1])
print("list1[-2]: ", list1[-2])
print("list1[-3]: ", list1[-3])
print("list1[-4]: ", list1[-4])
print("list1[-5]: ", list1[-5])
Output
输出量
list1[0]: 10
list1[1]: 20
list1[2]: 30
list1[3]: 40
list1[4]: 50
list1[-1]: 50
list1[-2]: 40
list1[-3]: 30
list1[-4]: 20
list1[-5]: 10
使用列表切片访问元素 (Access elements using List slicing)
We can also access a set of elements by using list slicing by defining the start_index and end_index.
我们还可以通过定义start_index和end_index来使用列表切片来访问一组元素。
Syntax:
句法:
list_object[[start]:[end])
Note: One of the values start or end may optional – consider the below example.
注意:值start或end可能是可选的-请考虑以下示例。
Example:
例:
# declaring lists
list1 = [10, 20, 30, 40, 50]
# printing list
print("list1: ", list1)
# printing elements using list slicing
# prints 5 elements from starting
print("list1[:5]: ", list1[:5])
# prints 3 elements from starting
print("list1[:3]: ", list1[:3])
# prints all elements from the index 0
print("list1[0:]: ", list1[0:])
# prints all elements from the index 3
print("list1[3:]: ", list1[3:])
# prints the elements between index 2 to 3
print("list1[2:3]: ", list1[2:3])
# prints the elements between index 0 to 4
print("list1[0:4]: ", list1[0:4])
# prints the elements between index 1 to 4
print("list1[1:4]: ", list1[1:4])
# prints elements in the reverse order
print("list1[ : : -1]: ", list1[ : : -1])
Output
输出量
list1: [10, 20, 30, 40, 50]
list1[:5]: [10, 20, 30, 40, 50]
list1[:3]: [10, 20, 30]
list1[0:]: [10, 20, 30, 40, 50]
list1[3:]: [40, 50]
list1[2:3]: [30]
list1[0:4]: [10, 20, 30, 40]
list1[1:4]: [20, 30, 40]
list1[ : : -1]: [50, 40, 30, 20, 10]
翻译自: https://www.includehelp.com/python/accessing-a-list-and-its-elements.aspx