简体   繁体   English

在Python中访问多维列表

[英]Accessing multi-dimensional list in Python

I have a list as follows in my python script: 我的python脚本中有以下列表:

a = [["iguana","i"],["mycat","m"]]

I want to access individual elements of the list and print them: 我想访问列表的各个元素并打印它们:

print a[0,0]
print a[1,1]

But this throws "TypeError: list indices must be integers, not tuple". 但这会引发“ TypeError:列表索引必须是整数,而不是元组”。

How can I access individual elements of the list? 如何访问列表中的各个元素?

Thanks 谢谢

Index them one at a time: 一次将它们编入索引:

>>> a = [["iguana","i"],["mycat","m"]]
>>> a[0]
['iguana', 'i']
>>> a[0][0]
'iguana'
>>> a[1][0]
'mycat'
>>>

The first [n] indexes list a , which returns a list, and the second indexes that list. 第一个[n]索引列出a ,它返回一个列表,第二个索引列出列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM