0
Follow
0
View

I want to sort the subarrays in a NumPy array that I have according to their length

cica1987 注册会员
2023-01-25 17:44

That is a simple list of lists - to do what you need:

sorted(myArray, key=lambda x: -len(x))

: if you have a numpy array, as follows:

myArray = np.array([np.array(x) for x in [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11], [12], [13], [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]]])

You can obtain a similar result:

np.array([y for x,y in sorted([(-len(x), x) for x in myArray])])

OUTPUT:

array([array([14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]),
   array([1, 2, 3, 4, 5, 6, 7]), array([ 8,  9, 10, 11]), array([12]),
   array([13])], dtype=object)

About the Author

Question Info

Publish Time
2023-01-25 17:44
Update Time
2023-01-25 17:44

Related Question

pytorch不调用gpu运算

Unity Photon Pun2如何实现网络物体交互?

wkhtmltopdf将网页转为pdf的命令

What is the appropriate update to 3.1 for UseWebpackDevMiddleware?

在气流中运行并行DockerOperator任务给出409客户端错误

没有子路径定义的ParameterStore访问

关于PRTG Netwoek Monitor本地部署后第二天开机显示本地探针已断开链接的问题,如何解决?

elementor form表单提示如何更改

JButtons和jpanel的意外行为

silicon laboratories ide