热线电话:13121318867

登录
2019-02-14 阅读量: 680
python如何从列表中访问元素

在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])

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子