我想迭代我的列表列表并迭代每个嵌套列表中的每个项目。
下面是我的一个列表列表的示例(只是一个示例 - 我的一些列表列表中有一个列表,其他列表最多为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
关于我做错了什么以及如何实现这一点的任何一点都将非常感激。
答案 0 :(得分:8)
你可以这样做。
>>> for i in coord:
for j in i:
print j
1231778.27
4953975.2109
1231810.4031
4953909.1625
...
等等。
答案 1 :(得分:3)
有很多方法可以做到这一点。
Т®迭代所有嵌套列表
for i in coord : for j in i : print j
通过itertools展平您的列表
import itertools for i in itertools.chain(*coord) : print i
通过缩小列表来展平您的列表
for i in sum(coord, []) : print i
答案 2 :(得分: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
的输出:
清单0的清单1 项目:
1
清单2中的第1号清单 项目:
1
2
清单3的清单2 项目:
1
2
3
清单4中的第3号清单 项目:
1
2
3
4
coord2
的输出:
第5号清单第5号 项目:
1
2
3
4
5
答案 3 :(得分:0)
在您的示例代码中,您的内部列表会显示一组坐标,这些坐标是一系列“x1, y1, x2, y2, ...
”值。
恕我直言,更可读的方法是使用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
这是一个处理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)