在pyhton如何访问列表中的元素“”
要访问列表项,用索引运算符[]访问列表中的项目。索引必须是整数。使用嵌套索引访问嵌套列表。
# Python program to demonstrate
# accessing of element from list
# Creating a List with
# the use of multiple values
List = ["Geeks", "For", "Geeks"]
# accessing a element from the
# list using index number
print("Accessing a element from the list")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'] , ['Geeks']]
# accessing a element from the
# Multi-Dimensional List using
# index number
print("Acessing a element from a Multi-Dimensional list")
print(List[0][1])
print(List[1][0])
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
# accessing a element using
# negative indexing
print("Acessing element using negative indexing")
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])








暂无数据