2

I have two arrays in Python (numpy arrays):

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

and I wish to create a third array with each element from b appearing a times in the new array, as:

c=array([1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,4,4,4,4,4])

Is there a fast, numPythonic way of doing this with a minimum of looping? I need to use this operation thousands of times in a loop over a fairly large array, so I would like to have it be as fast as possible.

Cheers, Mike

1 Answer 1

5

I believe repeat is what you want:

c = repeat(b, a)
Sign up to request clarification or add additional context in comments.

Comments

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.