简体   繁体   中英

Python: how to iterate over list of list while iterating over each item in each nested list

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

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.

You could just do.

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


1231778.27
4953975.2109
1231810.4031
4953909.1625
...

And so on.

There is a lot of ways to do that.

  1. То iterate all nested lists

     for i in coord : for j in i : print j 
  2. To flatten your list by itertools

     import itertools for i in itertools.chain(*coord) : print i 
  3. To flatten your list by reducing it

     for i in sum(coord, []) : print i 

This one is quiet simple. Look:

#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

Output for coord1 :

List No. 0 of leght 1
Items :
1
List No. 1 of leght 2
Items :
1
2
List No. 2 of leght 3
Items :
1
2
3
List No. 3 of leght 4
Items :
1
2
3
4

Output for coord2 :

List No. 0 of leght 5
Items :
1
2
3
4
5

From your example code, it appears your inner list represent group of coordinates which are a series of " x1, y1, x2, y2, ... " values.

IMHO, a more readable approach would be to use the grouper() itertools recipe :

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

Here's an example that handles the values in groups of 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)

For completeness, the grouper() method is defines as such:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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