Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Produce increments from a list to form an array

I have the following data

a= [1 1.1 1.2 1.3 1.4 1.5]

What I want to do is for each value of this data produce a series of points in increments with a spacing of 10%. Creating a new array:

b=  [[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0], [0 0.11 ... 1.1],.....]

The next thing that I want to do is is take each number from List 1 and determine the number of increments (20% spacing) from another value e.g. 2, to get another array:

c=[[1 1.2 1.4. 1.6 1.8 2.0], [1.1 ..... 2.0],......]

I then want to combine these arrays:

  d =[[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1 1.2 1.4. 1.6 1.8 2.0], [0 0.11 ... 2.0],.....]

List 1 is determined from an equation, but I want to do further calculations up to a certain point in this case a value of 2.

Would something arange work or some other way generating a sequence of numbers? Is this even possible?

like image 512
Jay Avatar asked Mar 22 '26 03:03

Jay


1 Answers

Mixing list comprehensions and np.linspace it is pretty straightforward:

>>> a = [1, 1.1, 1.2, 1.3, 1.4, 1.5]

>>> b = [np.linspace(0, j, 11) for j in a]
>>> b
[array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ]),
 array([ 0.  ,  0.11,  0.22,  0.33,  0.44,  0.55,  0.66,  0.77,  0.88,  0.99,
         1.1 ]),
 ...
 array([ 0.  ,  0.15,  0.3 ,  0.45,  0.6 ,  0.75,  0.9 ,  1.05,  1.2 ,  1.35,
         1.5 ])]

>>> c = [np.linspace(j, 2, 6) for j in a]
>>> c
[array([ 1. ,  1.2,  1.4,  1.6,  1.8,  2. ]),
 array([ 1.1 ,  1.28,  1.46,  1.64,  1.82,  2.  ]),
 ...
 array([ 1.5,  1.6,  1.7,  1.8,  1.9,  2. ])]

To concatenate them you must either remove the first element of every array in c or the last of every array in b. If you only need the concatenation, I would suggest keeping c as above, and doing:

>>> b = [np.linspace(0, j, 10, endpoint=False) for j in a]

>>> d = map(np.concatenate, zip(b, c))
>>> d
[array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
         1.2,  1.4,  1.6,  1.8,  2. ]),
 array([ 0.  ,  0.11,  0.22,  0.33,  0.44,  0.55,  0.66,  0.77,  0.88,
         0.99,  1.1 ,  1.28,  1.46,  1.64,  1.82,  2.  ]),
 ...
 array([ 0.  ,  0.15,  0.3 ,  0.45,  0.6 ,  0.75,  0.9 ,  1.05,  1.2 ,
         1.35,  1.5 ,  1.6 ,  1.7 ,  1.8 ,  1.9 ,  2.  ])]

If you want lists instead of numpy arrays you can always do a final

>>> d = map(list, d)
like image 109
Jaime Avatar answered Mar 23 '26 15:03

Jaime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!