简体   繁体   English

列表中的列表

[英]Lists in a list

I am quite new to Python. 我对Python很陌生。 I have a list containing some more lists, but only in two dimension (eg List[a][b]). 我有一个包含更多列表的列表,但仅包含二维(例如List [a] [b])。 Now for every value [a] I want to access a certain value [b] (10 in this case). 现在,对于每个值[a],我想访问某个值[b](在这种情况下为10)。 For now it would be sufficent to just print every value List[a][10]. 现在,仅打印每个值List [a] [10]就足够了。 I tried: 我试过了:

for rec in List:
    print List[rec][10]

That gives me the error "TypeError: list indices must be integers, not list". 这给了我错误“ TypeError:列表索引必须是整数,而不是列表”。 However if I just try "print List[0][10]" it gives me the value I want. 但是,如果我只是尝试“打印List [0] [10]”,它将为我提供所需的值。 In my for-Loop isn't rec an integer? 在我的for-Loop中不是rec整数吗? How could I solve this problem? 我该如何解决这个问题?

Additional info: I am using Python 2.4.3 to be able to use the shapefile library that lets me access GIS data (my list). 附加信息:我正在使用Python 2.4.3来使用shapefile库,该库使我可以访问GIS数据(我的列表)。

for rec in List:
    print rec[10]

should work. 应该管用。

You need to use: 您需要使用:

for rec in List:
    print rec[10]

or 要么

for i range(len(List)):
    print List[i][10]

rec in your case is not an integer, it is the first item of list. rec在您的情况下不是整数,它是列表的第一项。 For you to use it as an integer you must add range in the for loop, like "for rec in range(0,len(List))" 为了将其用作整数,必须在for循环中添加范围,例如“ for rec in range(0,len(List))”

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

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