I don't understand this way to initialize a multidimensional array using a list of lists (Numpy):
np.array([range(i, i + 3) for i in [2, 4, 6]])
OUT:
array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])
How do you get 3, 5, 7 and 4, 6, 8?
I understand that with range(i, i+3) you are defining that the matrix will have three columns, but how can we understand in this code, that the values of each of the two columns are obtained by adding 1 to the value of the previous column ?.
The false appearance that 1 is being added, is made by the
range()
and the chosen numbers. What you are doing is the following:for
we take the first number in the list the twoOnce this is done, we repeat the same loop again.
The
for
brings us the next number in the list, in this case four , we create a range from four to seven and it returns us a list [4,5,6]. We have the second row.Finally we would start the loop again with the last number six and at the end we would get the third and last row: [6,7,8].
range() function
What the function
range()
does is create a range of numbers. At the beginning of the loop it would be a range of [2, 5). As you point out,i + 3
what it is giving you is the number of columns, because if you put a four, you would have 4 columns, since the range of numbers would be in the first case [2,6). And always on the loose, the range function would return four numbers.