Reputation: 113
What would be the quickest way to insert values into the correct position in a sorted numpy array?
For example, I would like to insert every value of b
into a
:
a = [1,1,2,4,7,7,11,13,13,13,15,20,25,26,27,30,45,70]
b = [5,7,9,45]
I've tried looping through a
for each value of b
and inserting it that way. I've also tried the bisect_left
method:
for i in b:
a.insert(bisect_left(a,i),i)
Both methods are too slow as I have hundreds of thousands of data elements to go through.
Any ideas?
Upvotes: 8
Views: 10892
Reputation: 98
For a more pythonic approach, You can use bisect.insort(your_list, your_value)
to insert a value into the correct position of a sorted list. Like this:
import bisect
a = [1,1,2,4,7,7,11,13,13,13,15,20,25,26,27,30,45,70]
b = [5,7,9,45]
for value in b:
bisect.insort(a, value)
# Now a == [1, 1, 2, 4, 5, 7, 7, 7, 9, 11, 13, 13, 13, 15, 20, 25, 26, 27, 30, 45, 45, 70]
Upvotes: 2
Reputation: 441
You could use searchsorted
and insert
:
a = numpy.array([1,1,2,4,7,7,11,13,13,13,15,20,25,26,27,30,45,70])
b = numpy.array([5,7,9,45])
ii = numpy.searchsorted(a, b)
a = numpy.insert(a, ii, b)
Upvotes: 13
Reputation: 6814
let's note n = len(a) and m = len(b)
,
Now given possible values of n and m, you can determine which solution is best, but don't expect to do better than that
Upvotes: 4
Reputation: 2857
You solution slow becaue you have a lot of inserts. Each insrt is O(N) complexity.
My solution: a = [1,1,2,4,7,7,11,13,13,13,15,20,25,26,27,30,45,70] b = [5,7,9,45]
insert b.Length items into end of a. a = [1,1,2,4,7,7,11,13,13,13,15,20,25,26,27,30,45,70,x,x,x,x] b = [5,7,9,45]
Take 3 pointers:
a
Here my solution in C#:
int p1 = a.Length - 1;
int p2 = b.Length - 1;
int p3 = a.Length + b.Length - 1;
//Insert b.Length items to end of a.
while (p3 >= 0 && p2 >= 0)
{
if (p1 < 0 || b[p2] >= a[p1])
{
a[p3--] = b[p2--];
}
else
{
a[p3--] = a[p1--];
}
}
Upvotes: -3
Reputation: 239473
Just use builtin sort
method. It implements timsort
. If the list is almost sorted, it will be very fast.
a.extend(b)
a.sort()
Upvotes: 4