有什么方法可以使用numpy函数在mergesort中进行合并吗?
Is there any way we can do something like merge in mergesort using numpy function?
一些功能,例如合并:
a = np.array([1,3,5]) b = np.array([2,4,6]) c = merge(a, b) # c == np.array([1,2,3,4,5,6])我希望借助numpy能够获得大数据的高性能
I wish I could get high performance for large data thanks to numpy
推荐答案您可以使用
from numpy import concatenate, sort c = concatenate((a,b)) c.sort(kind='mergesort')除非您编写自己的排序功能作为python扩展名,否则恐怕您无法做得更好,如cython.
I am afraid you can't do better than this, unless you write your own sorting function as a python extension, à la cython.
有关类似问题,请参见此问题,但仅在其中保留唯一值合并的数组.那里的基准和评论也很有见地.
See this question for a similar problem, but keeping only the unique values in the merged array. The benchmarks and comments there are insightful as well.