我有一个排序列表——实际上是一个按 x 排序的 (x,y,z) 三元组的巨大数组。我的目标是根据 x 的范围将它分成几部分。我一直在努力
for triple in hugelist:
while triple[0] >= minx and triple[0] < maxx:
#do some stuff
# when out of that range, increase endpoints to the next range
minx = minx + deltax
maxx = maxx + deltax
# do some other stuff
# and hopefully move to next triple
现在当然这不起作用,因为我误用了 while,我明白为什么。但是,我不知道如何通过列表。hugelist 是大约 200 万个三元组,可以分成大约 600 个块。如果可能的话,我希望按顺序通过它一次。
===============================
在 Tim 的帮助下,使用 291 点迷你列表,bisect 错过了 maxx 应该去的地方:
while xstart < len(heights):
xfinish = bisect.bisect_left(heights, (maxx, 0, 0), lo=xstart)
xslice = heights[xstart:xfinish]
print "xstart is ", xstart, " xfinish is ", xfinish
print "maxx is ", maxx, " xslice is ", xslice
maxx += deltax
xstart = xfinish
xstart is 0 xfinish is 291
maxx is 804.0 xslice is [(803.01, 1941.84, 0.74) (803.04, 1941.88, 0.45) (803.06, 1941.25, 0.0)
(803.07, 1941.01, 0.0) (803.07, 1941.52, 0.31) (803.09, 1941.16, 0.08)
(803.12, 1940.05, 0.0) (803.13, 1939.72, 0.3) (803.13, 1939.86, 0.11)
(803.13, 1940.29, 0.17) . . . (803.23, 1938.24, 0.2)
(803.23, 1938.25, 0.45) (803.23, 1938.29, 0.1) (803.23, 1938.36, 0.0)
(803.23, 1938.49, 0.0) (803.96, 1941.06, 4.21) (**803.98**, 1940.6, 4.55)
(**804.0**, 1940.32, 4.49) (**804.01**, 1940.68, 4.6) . . . (806.11, 1934.82, 10.64)
(806.11, 1934.86, 10.65) (806.11, 1934.91, 10.56) (806.32, 1933.24, 4.69)]