点(x,y)坐标在Python中从低到高排序

点(x,y)坐标在Python中从低到高排序,python,list,algorithm,numpy,random,Python,List,Algorithm,Numpy,Random,“点”列表中的随机点。像这样:[x,y]=([3,4],[2,5],[0,7],[8,1]) 但我需要做这些过程 首先,我必须对列表中的点进行排序,比如将低Y和低X作为双点排序(例如[1,2],[1,3]) 第二,我怎样才能在列表中做这样的动作;(Y0-Y1/X0-X1) 这是我的密码 import numpy as np points = np.random.randint(0, 9, size=(18,2)) print(points) 似乎要使用X字段作为主键,Y字段作为辅助键对列表

“点”列表中的随机点。像这样:[x,y]=([3,4],[2,5],[0,7],[8,1])

但我需要做这些过程

  • 首先,我必须对列表中的点进行排序,比如将低Y和低X作为双点排序(例如[1,2],[1,3])

  • 第二,我怎样才能在列表中做这样的动作;(Y0-Y1/X0-X1)

这是我的密码

import numpy as np
points = np.random.randint(0, 9, size=(18,2))
print(points)

似乎要使用X字段作为主键,Y字段作为辅助键对列表进行排序。我为这个排序提供了lambda函数,但实际上这是排序函数的默认行为

不知道如何处理被零除的情况,因此对于垂直段返回
None

s =  [ [1,2] , [ 1,3] ,[ 5,4],[5,5] ]
s = sorted(s, key = lambda a: (a[0], a[1]))
#s = sorted(s) should give the same result
print(s)
slopes = [(a[1] - b[1]) / (a[0] - b[0]) if (a[0] - b[0])!=0 else None 
              for a, b in zip(s[:-1], s[1:])]
print(slopes)

[[1, 2], [1, 3], [5, 4], [5, 5]]
[None, 0.25, None]

我不理解您的排序方法。@Mbo例如,您有这样一个列表[[1,2]、[5,4]、[5,5]、[1,3]]-->排序列表,其x和y值较低。像这样:[[1,2],[1,3],[5,4],[5,5]-->在这个列表中,索引0=[1,2],索引1=[1,3]-->我正在尝试计算Y0-Y1/X0-X1。在这个例子中(2-3/1-1)。