I was hoping there was a better/faster way but a nested for loop seems kind of necessary.
python - Create 2D numpy array through nested loop - Stack Overflow
python - Understanding creating a 2D list with nested loops - Stack Overflow
Nested for Loops in Python- creating a two dimensional array/list of lists - Stack Overflow
Arrays, 2D Arrays and nested for loops
Videos
The numpy array you are defining is not the right shape for the loop you are using. b = np.array([]) gives you an array of shape (0,)
You can use something like np.zeros to define your 2D array.
import numpy as np
b = np.zeros((3,6))
for i in range(2):
for j in range(5):
b[i][j]=i+j
print(b)
The output will be
[[0. 1. 2. 3. 4. 0.]
[1. 2. 3. 4. 5. 0.]
[0. 0. 0. 0. 0. 0.]]
Another option is to make a 2D list, fill it up in for loops, and convert to numpy array later
import numpy as np
#2D array
b = [ [0 for _ in range(6)] for _ in range(3)]
#Fill up array with for loops
for i in range(2):
for j in range(5):
b[i][j]=i+j
#Convert to numpy array
print(np.array(b))
The output will be
[[0 1 2 3 4 0]
[1 2 3 4 5 0]
[0 0 0 0 0 0]]
I understand you asked how to do this with nested loops, so apologies if you don't find this useful. I'd just thought I'd share how I normally do this:
b = np.arange(10).reshape(2,5)
print(b)
[[0 1 2 3 4]
[5 6 7 8 9]]
It is just a shortcut for this:
x = []
for j in range(column):
_ = []
for i in range(row):
_.append(i)
x.append(_)
you can put the inner for loop into one by saying that the list is composed of a whole bunch of 0's. We do that for each i in range(row), so we can say _ = [0 for i in range(row)]. This is how that looks:
x = []
for j in range(column):
x.append([0 for i in range(row)])
We can now see that x is composed of a bunch of [0 for i in range(row)]'s and we do that once for each of j in range(column), so we can simplify it to this:
x = [[0 for i in range(row)] for j in range(column)]
Sometime to expand all the one-liner shorthand code can help you understand better:
Basic
x = [i for i in range(somelist)]
Can be expanded into:
x = []
for i in range(somelist):
x.append(i)
More Nested
x = [[0 for i in range(row)] for j in range(col)]
Can be expanded into:
x = []
for j in range(col):
_ = []
for i in range(row):
_.append(0)
x.append(_)
Note this:
>>> matrix = [[0] * 3] * 3
>>> [x for x in matrix]
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> [id(x) for x in matrix]
[32484168, 32484168, 32484168]
>>>
Three rows but only one object.
See the docs especially Note 2 about the s * n operation.
Fix:
>>> m2= [[0] * 3 for i in xrange(5)]
>>> [id(x) for x in m2]
[32498152, 32484808, 32498192, 32499952, 32499872]
>>>
Update: Here are some samples of code that gets an answer simply (i.e. without iter()):
>>> nrows = 2; ncols = 4
>>> zeroes = [[0 for j in xrange(ncols)] for i in xrange(nrows)]
>>> zeroes
[[0, 0, 0, 0], [0, 0, 0, 0]]
>>> ap = [[ncols * i + j for j in xrange(ncols)] for i in xrange(nrows)]
>>> ap
[[0, 1, 2, 3], [4, 5, 6, 7]]
>>>
Try running matrix[0][0] = 0 after that. Notice it now becomes:
[[0, 7, 8], [0, 7, 8], [0, 7, 8]]
So it's changing all three at the same time.
Read this: http://www.daniweb.com/software-development/python/threads/58916
That seems to explain it.