我下面的Python代码很慢,有没有可能用Numpy语句完全写出这部分?
m = len(self.man_list)
s = len(self.newnew)
self.zeroMatrix = np.zeros((m,s))
for k in range(m):
a1 = self.man_list[k][2]
b1 = self.man_list[k][0]
a2 = self.man_list[k][3]
b2 = self.man_list[k][1]
for f, part in enumerate(self.extra_list):
x1 = self.extra_list[f][0]
y1 = self.extra_list[f][2]
x2 = self.extra_list[f][1]
y2 = self.extra_list[f][3]
first = np.array((x1, y1))
second = np.array((x2, y2))
third = np.array((a1, b1))
forth = np.array((a2, b2))
dist1 = np.linalg.norm(first - third)
dist2 = np.linalg.norm(second - forth)
distance = (dist1 + dist2)
self.zeroMatrix[k][f] = distance
首先,我创建了一个零 ( self.zeroMatrix)矩阵。
self.man_list并self.extra_list包含线的起点和终点坐标点。例如:
self.man_list = [ [ [1,2], [3,4] ],...]
self.extra_list = [ [ [11,30], [4, 10] ],...]
我得到从第一个列表的每一行到另一个列表的每一行的距离,然后我在self.zeroMatrix.
我非常感谢您的回答!
相关分类