Python:如何遍历列表列表,同时遍历每个嵌套列表中的每个项目 [英] Python: how to iterate over list of list while iterating over each item in each nested list

查看:2460
本文介绍了Python:如何遍历列表列表,同时遍历每个嵌套列表中的每个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要遍历列表列表,并遍历每个嵌套列表中的每个项目.

I want to iterate over my list of list and iterate over each item in each nested list.

以下是我的一个列表列表的示例(仅是一个示例-我的某些列表列表包含1个列表,其他列表最多5个):

below is an example of one of my list of lists (just an example - some of my lists of lists have 1 list others up to 5):

coord = [['1231778.27', '4953975.2109', '1231810.4031', '4953909.1625', '1231852.6845', '4953742.9888', '1231838.9939', '4953498.6317', '1232017.5436', '4953273.5602', '1232620.6037', '4953104.1389', '1233531.7826', '4953157.4443', '1233250.5928', '4952272.8482', '1233023.1992', '4951596.608', '1233028.445', '4951421.374', '1233113.3502', '4950843.6951', '1233110.1943', '4950224.8384', '1232558.1541', '4949702.3571', '1232009.4781', '4949643.5194', '1231772.6319', '4949294.7085', '1232228.9241', '4948816.677', '1232397.6873', '4948434.382', '1232601.4467', '4948090.1894', '1232606.6477', '4947951.0044', '1232575.7951', '4947814.7731', '1232577.9349', '4947716.6405', '1232581.1196', '4947587.4665', '1232593.5356', '4947302.0895', '1232572.993', '4947108.3982', '1232570.8043', '4947087.7615'],['1787204.7571', '5471874.7726', '1787213.6659', '5471864.3781', '1787230.0001', '5471864.3772', '1787238.9092', '5471870.3161']]

以下是我到目前为止提出的内容,但是在访问第二个列表时遇到问题.在这个阶段,我只是打印以解决问题,但计划将这些值传递给函数.

below is what I have come up with so far but I am having problems access the second list. at this stage I am just printing to trouble shoot but plan to pass these values to a function.

for i in range(0,len(coord),):
    coord = coord[i]


    for j in range(0,len(coord[:-3]),2):
            x1 = coord[j]
            y1 = coord[j+1]
            x2 = coord[j+2]
            y2 = coord[j+3]
            print x1, y1, x2, y2

关于我在做什么错以及如何实现这一点的任何观点,将不胜感激.

any points as to what I am doing wrong and how to achieve this would be much appreciated.

推荐答案

您可以做到.

>>> for i in coord:
        for j in i:
            print j


1231778.27
4953975.2109
1231810.4031
4953909.1625
...

以此类推.

这篇关于Python:如何遍历列表列表,同时遍历每个嵌套列表中的每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆