yanci
yanci

Reputation: 173

Numpy merging arrays

I have two arrays

 a = np.array[1,18,3,13,6,45,45]
 b= np.array [8,13,6,45,45]

after doing some matching exercises I have a list, such as:

[3,1,4]

first and second number of the list are zero based. the third number is not. the list stands for

so for this example the result would be

13, 13 
6,6
45,45
45,45

Both starting points of the array and then get 4 rows after that.

How can I merge my two arrays using my matchlist?

EDIT This is the matchlist that I am using:

matchlist2 = []

matchlist2.append([3,1,4])
matchlist2.append([9,7,731])
matchlist2.append([766,762,19])
matchlist2.append([800,796,57])
matchlist2.append([867,862,88])
matchlist2.append([960,955,468])
matchlist2.append([1432,1427,65])
matchlist2.append([1523,1518,341])
matchlist2.append([1873,1868,32])
matchlist2.append([1923,1916,82])
matchlist2.append([2011,2004,699])
matchlist2.append([2716,2707,902])
matchlist2.append([3628,3617,247])
matchlist2.append([3923,391,378])
matchlist2.append([4306,4292,5])

Upvotes: 0

Views: 96

Answers (2)

zipa
zipa

Reputation: 27869

This is what you need:

a = np.array([1,18,3,13,6,45,45])
b = np.array([8,13,6,45,45])

look = [3,1,4]
first, second, step = look
c = np.array(zip(a[first:first+step],b[second:second+step]))
c
#[[13 13 
# [ 6  6
# [45 45
# [45 45]]

Upvotes: 0

Saravana Kumar
Saravana Kumar

Reputation: 482

I would do it something like this:

result = np.array([[[a[ind[0]],b[ind[1]]] for ind in zip(range(ml[0],ml[0]+ml[2]),range(ml[1],ml[1]+ml[2]))] for ml in matchlist2])

Edit: Divakar's solution is actually much more elegant. If you just zip it up you'd get what you need.

result = [list(zip(a[l[0]:l[0]+l[2]],b[l[1]:l[1]+l[2]])) for l in matchlist2]

Upvotes: 1

Related Questions