user6858243 - 4 months ago 44

Python Question

I would like to create a new variable in a loop with an index in which I write a 2d matrix of data. Something like this:

`import numpy`

DARK = []

a = []

for i in range(0,3):

# create 3d numpy array

d = numpy.array([[1, 2], [3, 4]])

a.append(d)

stack = numpy.array(a)

# write it into the actual variable (here is the problem)

DARK[i] = numpy.median(stack)

I tried an approach with DARK.append but that gave me an list index out of range error.

Answer

After 4 days of trying I found the answer myself. Thanks for the great help guys...

```
import numpy
DARK = []
a = []
stack = []
for i in range(0,3):
# create 3d numpy array
d = numpy.array([[1, 2], [3, 4]])
a.append(d)
stack.append(numpy.array(a))
# write it into the actual variable
DARK.append(numpy.array(numpy.median(stack[i], 0)))
```