0

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

  • start position of first array

  • start position of second array

  • how many numbers rows to retrieve

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])
3
  • Given, l = [3,1,4] : a[l[0]:l[0]+l[2]] , b[l[1]:l[1]+l[2]]? Commented Dec 14, 2017 at 22:17
  • What data structure would your result have to be in? A 2 column array? Or would you need a list of tuples? Commented Dec 14, 2017 at 22:17
  • @Saravana Kumar , it would have to be in a 2 column array thanks! Commented Dec 14, 2017 at 22:22

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

10 Comments

I am getting this error: TypeError: slice indices must be integers or None or have an index method. I may just be doing something wrong though
Well can you see what your matchlist looks like? For some reason python thinks the elements of your matchlist are not integers.
I included the matchlist that I am using
Alright so your matchlist is actually a 2D array. Let me edit my answer.
How do you display the contents of the merged list? I only see "<zip object at 0x......"
|
0

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]]

1 Comment

I am using spyder, and when I get 'object arrays are currently not supported'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.