1

我想遍历我的列表列表并遍历每个嵌套列表中的每个项目。

下面是我的列表列表之一的示例(只是一个示例 - 我的一些列表列表有 1 个列表,其他列表最多 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']]

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

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

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

4

4 回答 4

8

你可以这样做。

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


1231778.27
4953975.2109
1231810.4031
4953909.1625
...

等等。

于 2013-05-20T11:38:53.003 回答
3

有很多方法可以做到这一点。

  1. То 迭代所有嵌套列表

    for i in coord :
        for j in i :
            print j
    
  2. 通过 itertools 展平您的列表

    import itertools
    for i in itertools.chain(*coord) :
          print i
    
  3. 通过减少列表来展平您的列表

    for i in sum(coord, []) :
        print i
    
于 2013-05-20T12:24:41.273 回答
0

从您的示例代码中,您的内部列表似乎代表一组坐标,它们是一系列“ x1, y1, x2, y2, ...”值。

恕我直言,更易读的方法是使用grouper()itertools 配方

for row in coord:
    for x1, y1, x2, y2 in grouper(row, 4):  # assuming groups of 4
        # use x1, x2, x3, x4 here

这是一个处理 2 个一组的值的示例:

>>> for i, row in enumerate(coord):
...     for x, y in grouper(row, 2):
...         print "row=%d, (%s, %s)" % (i, x, y)
... 
row=0, (1231778.27, 4953975.2109)
row=0, (1231810.4031, 4953909.1625)
row=0, (1231852.6845, 4953742.9888)
row=0, (1231838.9939, 4953498.6317)
row=0, (1232017.5436, 4953273.5602)
row=0, (1232620.6037, 4953104.1389)
row=0, (1233531.7826, 4953157.4443)
row=0, (1233250.5928, 4952272.8482)
row=0, (1233023.1992, 4951596.608)
row=0, (1233028.445, 4951421.374)
row=0, (1233113.3502, 4950843.6951)
row=0, (1233110.1943, 4950224.8384)
row=0, (1232558.1541, 4949702.3571)
row=0, (1232009.4781, 4949643.5194)
row=0, (1231772.6319, 4949294.7085)
row=0, (1232228.9241, 4948816.677)
row=0, (1232397.6873, 4948434.382)
row=0, (1232601.4467, 4948090.1894)
row=0, (1232606.6477, 4947951.0044)
row=0, (1232575.7951, 4947814.7731)
row=0, (1232577.9349, 4947716.6405)
row=0, (1232581.1196, 4947587.4665)
row=0, (1232593.5356, 4947302.0895)
row=0, (1232572.993, 4947108.3982)
row=0, (1232570.8043, 4947087.7615)
row=1, (1787204.7571, 5471874.7726)
row=1, (1787213.6659, 5471864.3781)
row=1, (1787230.0001, 5471864.3772)
row=1, (1787238.9092, 5471870.3161)

为了完整起见,该grouper()方法定义如下:

from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)
于 2013-05-21T08:17:05.677 回答
0

这个很简单。看:

#Just two lists 
coord1 = [ [1], [1,2], [1,2,3], [1,2,3,4]  ]
coord2 = [ [1, 2, 3, 4, 5] ]

#iterate over the list of lists
for every_list in coord1:
    print "List No. %i of leght %i" % (coord1.index(every_list), len(every_list))
    print "Items :"

    #print all items of actual list
    for item in every_list:
        print item

输出coord1

第 1 项第 0项清单
项目:
1第 2 项
第 1项清单

1
2
第 3 项第 2 项清单
项目:
1
2
3
第 4 项第 3 项清单
项目:
1
2
3
4

输出coord2

清单编号 0,共 5
项 :
1
2
3
4
5

于 2013-05-20T12:24:33.380 回答