numpy merge sorted array to an new array?

前端 未结 3 722
有刺的猬
有刺的猬 2020-12-11 14:58

Is there any way we can do something like merge in mergesort using numpy function?

some function like merge:

a = np.array([1,3,5])
b = np.array([2,4         


        
3条回答
  •  旧时难觅i
    2020-12-11 15:25

    You can use

    from numpy import concatenate, sort
    
    c = concatenate((a,b))
    c.sort(kind='mergesort')
    

    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.

提交回复
热议问题