2

Given a list/array of elements, such as [0, 1, 2], what is the most numpythonic way to replicate each element n times and store the result in a 1-d array, such as (for n=3) [0, 0, 0, 1, 1, 1, 2, 2, 2]?

1 Answer 1

2

Use np.repeat:

In [86]: np.repeat([0, 1, 2], 3)
Out[86]: array([0, 0, 0, 1, 1, 1, 2, 2, 2])
Sign up to request clarification or add additional context in comments.

1 Comment

I knew I missed the obvious solution. Thanks!

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.