Skip to content Skip to sidebar Skip to footer

Referring To Arrays In A For-loop

Suppose i have 3 Numpy arrays containing floats with the following names: one two three. Is it possible to refer to them in a for-loop in the following manner: list=one two three

Solution 1:

You need to specify where you'd like to save the value:

arr[2,1] = arr[2,1] + 1

or just:

arr[2,1] += 1

So the whole code becomes:

import numpy as np
one = np.arange(6).reshape(3, 2)
two = np.arange(10).reshape(5, 2)
arrays = [one, two]
for arr in arrays:
    arr[2, 1] += 1

Solution 2:

I think it would work if you made your list variable a list:

list=[one, two, three]
for arr in list:
    arr=arr[2,1]+1

Post a Comment for "Referring To Arrays In A For-loop"