我有几个复数,我需要根据它们的欧几里德距离排序。我是这样解决这个问题的:
# A1x = Lowest Point (LP)
# B1x = Point 1 (P1)
# B4x = Point 2 (P2)
C1 = euclidean(A1x, B1x) # Build the distance between LP and P1
C4 = euclidean(A1x, B4x) # Build the distance between LP and P2
array = np.array([C1, C4]) # Put the distances into an array...
array.sort() # ...and sort it.
# If the the distance between LP and P1 is the first element in the array
# then take P1 as y_max value etc.
if C1 == array[0]:
y_max = B1x
if C4 == array[1]:
y_min = B4x
if C4 == array[0]:
y_max = B4x
if C1 == array[1]:
y_min = B1x
这种方法对三到四个点很有效。但是,现在我得到了8到9个点,上面所说的方法有点讨厌,因为我必须为每一个点写if条件。因此,我想问你是否知道一种更好的方法来根据复数的欧几里德距离对它们进行排序。在
使用
argsort
,这很简单:
^{pr2}$sorted_B
最终包含列表B中的点,但按到点Ap的欧几里德距离排序。对于上面的输入,您将得到输出注意,以这种方式使用NumPy函数应该比使用等效的Python函数
list.sort
更快、更高效。在也可以使用
abs
代替euclidean
。在相关问题 更多 >
编程相关推荐