排序列表的numpy数组

时间:2019-12-08 12:11:21

标签: python numpy vectorization

我们获得了列表(ndarray)的numpy数组(dtype=object),并希望返回类似的列表数组,其中每个列表都经过排序。有没有一种有效的方法(即没有for循环等)?

请不要提供np.vectorize()作为解决方案,因为它被实现为for循环,因此效率低下。

例如:

a=np.array([[5,4],[6,7,2],[8,1,9]],dtype=object)

所以a是:

array([list([5, 4]), list([6, 7, 2]), list([8, 1, 9])], dtype=object)

,我们希望函数对其进行排序,这样我们就可以得到:

array([list([4, 5]), list([2, 6, 7]), list([1, 8, 9])], dtype=object)

2 个答案:

答案 0 :(得分:4)

您的示例,以及用于时间测试的扩展版本:

In [202]: a=np.array([[5,4],[6,7,2],[8,1,9]],dtype=object)                      
In [203]: A = a.repeat(100)                                                     

将Python列表排序应用于每个元素:

In [204]: np.array([sorted(i) for i in a])                                      
Out[204]: array([list([4, 5]), list([2, 6, 7]), list([1, 8, 9])], dtype=object)

使用frompyfunc进行相同操作:

In [205]: np.frompyfunc(sorted,1,1)(a)                                          
Out[205]: array([list([4, 5]), list([2, 6, 7]), list([1, 8, 9])], dtype=object)

一些时间:

In [206]: timeit np.array(list(map(sorted, A)))                                 
168 µs ± 221 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [207]: timeit np.array([sorted(i) for i in A])                               
181 µs ± 249 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

map比列表理解要快一点。我更喜欢理解的可读性。

纯列表版本要快很多:

In [208]: %%timeit temp=A.tolist() 
     ...: list(map(sorted, temp)) 
     ...:  
     ...:                                                                       
88.3 µs ± 70.8 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

frompyfunc比数组映射更快,并且几乎与纯列表版本一样好:

In [209]: timeit np.frompyfunc(sorted,1,1)(A)                                   
97.3 µs ± 1.93 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

这就是我以前见过的模式。 frompyfunc是将函数应用于对象dtype数组的元素的最快方法,但是它很少比基于列表的迭代好。

答案 1 :(得分:1)

np.array(list(map(sorted, a)))

给予:

array([list([4, 5]), list([2, 6, 7]), list([1, 8, 9])], dtype=object)