2

How do you create a 1 x n array in NumPy following an incrementing pattern?

For example:

[0, 5, 10, 15, ... (n-1)*5]

3
  • np.arange(n) * 5 where you import numpy as np first Commented Dec 28, 2018 at 20:26
  • Or np.arange(0, N+1, 5) Commented Dec 28, 2018 at 20:27
  • Should your example output be [0, 5, 10, 15, ... (n-1)*5]? What you currently have doesn't make complete sense. Commented Dec 28, 2018 at 21:05

1 Answer 1

1

np.arange is the correct answer (as pointed out in the comments). For completeness, here's a list of simple 1-liners that will produce the desired array:

  • np.arange(n)*5
  • np.arange(0, n*5, 5)
  • np.linspace(0, (n-1)*5, n, dtype=int)
  • np.array(range(0, n*5, 5))

For example, if n=7 then all of the above will produce the array:

[ 0  5 10 15 20 25 30]
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.