user6858243 - 1 year ago 133

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.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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)))
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**