【发布时间】:2018-03-26 03:47:18
【问题描述】:
我在每个数组中有一个包含 4 个坐标的数组
x0 = [1,2,3,4] #x_coordinates
y0 = [1,2,3,4] #y_coordinates
x1 = [11,12,13,14] #x_coordinates
y1 = [11,12,13,14] #y_coordinates
我想求两个坐标之间的距离。
distance = sqrt((x1 - x0)^2 + (y1 - y0)^2)
所以,我试过了
distance = math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
但错误是TypeError: only length-1 arrays can be converted to Python scalars.
仅使用 array_variable 是否可以进行元素明智的操作?还是我必须使用 for 循环对其进行迭代?
我发现这是一个可能的答案,但 numpy 看起来相当复杂。 calculating distance between two numpy arrays
编辑:
尝试了以下
x_dist = pow((x1 - x0), 2)
y_dist = pow((y1 - y0), 2)
dist = x_dist+y_dist
dist=dist**2
【问题讨论】:
-
你不能在 python 中减去列表。考虑使用
numpy数组。 -
@DyZ :你确定,你不能在 python 中减去ists 吗? x1-x0 看起来完全是真实的声明。
-
是的,我确定。但是你确定你的 x1 和 x0 是 lists 吗?
-
@DyZ 是的,它是.. print x0 给了我 [413.59921265 412.74182129 411.94470215 411.37411499]。请查看我编辑的答案,它不再给出错误。所以我很困惑为什么它可以工作,当它不可能在 python 中减去列表时。
-
对不起它的
而不是一个列表!我现在明白了。