5

So I have an array of 100 elements:

a = np.empty(100)

How do I fill it with a range of numbers? I want something like this:

b = a.fill(np.arange(1, 4, 0.25))

So I want it to keep filling a with that values of that range on and on until it reaches the size of it

Thanks

hpaulj
  • 221,503
  • 14
  • 230
  • 353
hpnk85
  • 395
  • 1
  • 7
  • 12

3 Answers3

8

np.put places values from b into a at the target indices, ind. If v is shorter than ind, its values are repeated as necessary:

import numpy as np

a = np.empty(100)
b = np.arange(1, 4, 0.25)
ind = np.arange(len(a))
np.put(a, ind, b)
print(a)

yields

[ 1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.    3.25  3.5   3.75
  1.    1.25  1.5   1.75]
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
4

updating solution to fit description

a = np.empty(100)
filler = np.arange(1,4,0.25)
index = np.arange(a.size)
np.put(a,index,filler)
Ravi Sankar Raju
  • 2,850
  • 3
  • 18
  • 16
  • Thanks.. but I need the size of the array to be fixed, in that case it works because it meets 100 elements. But if we put instead of 0.25 steps of 5; we end up with an array of 5 elements. What I need in this case is the array to keep recording the elements until it reaches its size – hpnk85 May 27 '16 at 02:31
  • I don't understand the requirement. Array size, step size and starting value are all fixed, right ? – Ravi Sankar Raju May 27 '16 at 02:32
  • Yes I need to have 100 elements and they have to be within a certain range – hpnk85 May 27 '16 at 02:34
  • so the step does not matter ? if the range is 1 to 5, step should be calculated accordingly so that it fills up 100 elements ? – Ravi Sankar Raju May 27 '16 at 02:36
  • If the range is let's say (1,6,2) I need the array to have 100 elements of [1,3,5,1,3,5....3] – hpnk85 May 27 '16 at 02:37
  • got it. give me a min – Ravi Sankar Raju May 27 '16 at 02:38
2

Such a simple feat can be achieved in plain python, too

>>> size = 100
>>> b = [v/4 for v in range(4,16)]
>>> a = (b * (size // len(b) + 1))[:size]
>>> a
[1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 
 1.0, 1.25, 1.5, 1.75]

with these exact conditions it's about 3 times faster than numpy.

michelek
  • 2,380
  • 1
  • 13
  • 15