我必须通过大约1.300.000行的列表进行搜索。 时间顺序很重要!
它的形状如下:
Z X Y
18,139869,87718
18,139869,87719
18,139869,87722
18,139869,87725
18,139869,87726
18,139869,87751
18,139869,87752
18,139869,87841
18,139869,87842
18,139869,87843
18,139869,87844
18,139869,87845
18,139869,87846
18,139869,87847
18,139869,87861
18,139869,87862
18,139869,87882
18,139869,87886
18,139869,87887
18,139869,87888
18,139869,87889
18,139869,87890
18,139869,87891
18,139869,87902
18,139869,87912
18,139869,87913
18,139869,87914
18,139869,87918
18,139869,87919
18,139869,87933
18,139869,87934
18,139869,87936
18,139869,87938
这个列表我正在创建另一个列表:
18,139869,87718
18,139869,87719
18,139869,87722
18,139869,87723
18,139869,87725
18,139869,87727
18,139869,87751
18,139869,87753
18,139869,87841
18,139869,87848
依旧......
我在类方法中使用这个Python代码:
while idx in range(len(self.zoom_list)):
this_Xelement = self.zoom_list[idx]
next_Xelement = self.zoom_list[(idx + 1) % len(self.zoom_list)]
#get diffenrences in line ntries for x and y coordinate
X = int(next_Xelement[1]) - int(this_Xelement[1])
y = int(next_Xelement[2]) - int(this_Xelement[2])
#set start coordinate for Bounding Box
x_start = int(this_Xelement[1])
y_start = int(this_Xelement[2])
#decide witch coordinate to set as end coordinet for rendering Bounding Box
if X == 0:
if y > 1:
x_end = int(this_Xelement[1]) + 1
y_end = int(this_Xelement[2]) + 1
elif y == 1:
bidx = idx
for bidx, row in enumerate(self.zoom_list, bidx):
this_Yelement = self.zoom_list[bidx % len(self.zoom_list)]
next_Yelement = self.zoom_list[(bidx + 1) % len(self.zoom_list)]
y2 = int(next_Yelement[2]) - int(this_Yelement[2])
if y2 == 1:
continue
elif y2 > 1:
x_end = int(this_Xelement[1]) + 1
y_end = int(this_Yelement[2]) + 1
break
elif y2 < 1 and bidx == (len(self.zoom_list) - 1):
x_end = int(this_Xelement[1]) + 1
y_end = int(this_Yelement[2]) + 1
break
idx = bidx
elif X == 1:
x_end = int(next_Xelement[1]) + 1
y_end = int(this_Xelement[2]) + 1
else:
x_end = int(this_Xelement[1]) + 1
y_end = int(this_Xelement[2]) + 1
#create BB coordinates from extracted start and end tile coordinates
r_Up = self.num2deg(int(x_start), int(y_start), self.zoom)
l_Down = self.num2deg(int(x_end) - 0.25, int(y_end) - 0.25, self.zoom)
#create bounding box for rendering with mapnik (left down + right up)
bb = l_Down + r_Up
self.bb_list.append(bb)
idx += 1
有没有办法加快速度? 2:48h太长了。
答案 0 :(得分:4)
range
创建一个列表。 while谓词中的表达式导致每次迭代创建/销毁列表;创建1,300,000个项目,结果列出1,300,000次。
while idx in range(len(self.zoom_list)): # this is run every iteration
...
idx += 1
通过for
使用xrange
语句,它将更容易阅读,不会发生列表创建/销毁。
for idx in xrange(len(self.zoom_list)):
...
另一个小问题:有不必要的重复int
来电。
删除不必要的int
来电。例如int(y_start)
,int(y_end)
可以在y_start
之前替换为y_end
,y_start
,y_end
已经是int
个对象。< / p>
此外,如果可能,请事先将self.zoom_lsit
转换为包含int
,以避免重复int
次来电。
<强>更新强>
我注意到idx
循环中修改了while
。但是,第一次提及range
是有效的;避免重复range
次来电:
while 0 <= idx < len(self.zoom_list):
...
idx += 1