Malik
Malik

Reputation: 25

python Numpy arrays in array using nested for loop

I am trying create an numpy array that contain sub arrays. The output that i am looking for should be like this:

[[0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
 21.  22.5 24.  25.5 27.  28.5 30.] [0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5 21]......]

but instead i am getting just a single array as below

[0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
 21.  22.5 24.  25.5 27.  28.5 30. 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5 21........]

The background of this is that i have an array of arrays called "b".that array looks like this: [array([421.3, 448.6, 449.32171103, 444.28498751, 449.36065693, 448.75383007, 449.25048692, 448.75383007, 448.59001326, 448.64239657, 448.64239657, 448.00558032, 448.00558032, 447.93972809, 448.44620636, 447.93972809, 447.93972809, 447.87609894, 447.64383163, 447.6985593 , 447.21918563]), array([447.75551365, 447.75551365, 448.36146132, 447.75551365, 447.75551365, 447.75551365, 447.75551365, 448.36146132, 447.6985593 , 448.36146132, 447.6985593 , 447.59133146, 447.6985593 , 447.54105957, 447.64383163, 447.54105957, 446.87805943, 446.87805943, 446.75720475, 446.70012313, 446.70012313, 446.70012313, 446.64527312, 446.64527312, 446.14907822, 445.88002871, 445.70169396, 445.29989894]).........] i need to plot each array and i want to create another similar array of arrays with matching lengths but with different content using the code below. here is my code can you please suggest how to fix this.

tt=np.array([])

for i in range(len(array_size)):
    time_calc_1=0
    for j in range(len(b[i])):
    
        tt=np.append(tt,time_calc_1)
        time_calc_1=time_calc_1+1.5

Upvotes: 1

Views: 114

Answers (1)

Ehsan
Ehsan

Reputation: 12397

Do not loop and append to array. It is a bad idea. Instead use functions to achieve your goal:

tt = np.repeat(np.arange(0,31,1.5)[None,:],25,0)

output:

[[ 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
  21.  22.5 24.  25.5 27.  28.5 30. ]
 [ 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
  21.  22.5 24.  25.5 27.  28.5 30. ]
 [ 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
  21.  22.5 24.  25.5 27.  28.5 30. ]
...

UPDATE: In case of variable length arrays, (I suggest to use list of lists, but if array of arrays is insisted):

list of lists:

b = [10,20,30]
tt = [np.arange(0,i,1.5).tolist() for i in b]
#[[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0], [0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5], [0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5, 21.0, 22.5, 24.0, 25.5, 27.0, 28.5]]

array of arrays:

b = [10,20,30]
tt = np.array([np.arange(0,i,1.5) for i in b])
#[array([0. , 1.5, 3. , 4.5, 6. , 7.5, 9. ])
# array([ 0. ,  1.5,  3. ,  4.5,  6. ,  7.5,  9. , 10.5, 12. , 13.5, 15. ,  16.5, 18. , 19.5])
# array([ 0. ,  1.5,  3. ,  4.5,  6. ,  7.5,  9. , 10.5, 12. , 13.5, 15. ,  16.5, 18. , 19.5, 21. , 22.5, 24. , 25.5, 27. , 28.5])]

Upvotes: 1

Related Questions