Vofun - 5 months ago 34

Python Question

(this may look a little similar to "Combining 2-d arrays to form a 3-d array" but bear with me)

I want to combine several 2D numpy arrays together in a 3D numpy array, such that each array that is added is lies on top of the ones added before, like stacking papers, so that if I have the following arrays:

numpy.array_tot()

numpy.array_one((1,1,1),(2,2,2),(3,3,3),(4,4,4))

numpy.array_two((5,5,5),(6,6,6),(7,7,7),(8,8,8))

array_tot would have the value (((1,1,1),(2,2,2),(3,3,3),(4,4,4)),((5,5,5),(6,6,6),(7,7,7),(8,8,8))). Pretty simple right? And similar to the question "Combining 2-d arrays to form a 3-d array." Except that the values held in array_one and array_two are pretty much unknown, and instead of their being two arrays (array_one and array_two) with two values, there's one array and a whole lot of values, made in a loop so that each time it runs, it adds the next array, sort of like this:

(note: etc refers to many more, let's say 72 more, values that I haven't listed. file refers to a list of files I have neglected to mention)

`listx[50, 30, 90, etc]`

listy[39, 75, 680, etc]

data=np.array(file[0].data)

array_of_arrays

limit = 70

runs = 0

for runs in range(1,75):

x = listx([runs])

y = listy([runs])

runs += 1

array = sec_data[y - limit/2: y + limit/2, x - limit/2: x + l/2]

(and then whatever I would do to add array to array of arrays)

How do I keep adding arrays one by one to another array?

Answer

You should append the arrays onto a regular python list and then convert the list to a numpy array at the end:

```
import numpy as np
total = []
for i in range(5,15):
thisArray = np.arange(i)
total.append(thisArray)
total = np.asarray(total)
```

That loop makes a 2D array; you'd nest loops to produce higher dimensional arrays.