如何访问列表元素

2024-05-17 12:13:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张单子

list = [['vegas','London'],['US','UK']]

如何访问此列表的每个元素?


Tags: 元素列表list单子uslondonukvegas
0条回答
网友
1楼 · 发布于 2024-05-17 12:13:39

艰苦地学习python ex 34

试试这个

animals = ['bear' , 'python' , 'peacock', 'kangaroo' , 'whale' , 'platypus']

# print "The first (1st) animal is at 0 and is a bear." 

for i in range(len(animals)):
    print "The %d animal is at %d and is a %s" % (i+1 ,i, animals[i])

# "The animal at 0 is the 1st animal and is a bear."

for i in range(len(animals)):
    print "The animal at %d is the %d and is a %s " % (i, i+1, animals[i])
网友
2楼 · 发布于 2024-05-17 12:13:39

很简单

y = [['vegas','London'],['US','UK']]

for x in y:
    for a in x:
        print(a)
网友
3楼 · 发布于 2024-05-17 12:13:39

首先,我不调用它list,因为这是Python内置类型的构造函数的名称。

但一旦你把它改名为cities或其他东西,你就会:

print(cities[0][0], cities[1][0])
print(cities[0][1], cities[1][1])

相关问题 更多 >