You can try this:
import numpy as np
listvals = []
for i in range(10):
listvals.append((xc, yc))
mat_vals = np.vstack(listvals)
This will output an ndarray like this:
[[ 2 0]
[ 3 1]
[ 4 2]
[ 5 3]
[ 6 4]
[ 7 5]
[ 8 6]
[ 9 7]
[10 8]
[11 9]]
Or this maybe is better:
import numpy as np
list_xc = []
list_yc = []
for i in np.arange(10):
list_xc.append(xc)
list_yc.append(yc)
mat_xc = np.asarray(list_xc)
mat_yc = np.asarray(list_yc)
mat_f = np.column_stack((mat_xc, mat_yc))
Answer from Litwos on Stack OverflowYou can try this:
import numpy as np
listvals = []
for i in range(10):
listvals.append((xc, yc))
mat_vals = np.vstack(listvals)
This will output an ndarray like this:
[[ 2 0]
[ 3 1]
[ 4 2]
[ 5 3]
[ 6 4]
[ 7 5]
[ 8 6]
[ 9 7]
[10 8]
[11 9]]
Or this maybe is better:
import numpy as np
list_xc = []
list_yc = []
for i in np.arange(10):
list_xc.append(xc)
list_yc.append(yc)
mat_xc = np.asarray(list_xc)
mat_yc = np.asarray(list_yc)
mat_f = np.column_stack((mat_xc, mat_yc))
You can do this:
aList = []
for i in range(10):
aList.append([xc, yc])
loops - Iterating over a 2 dimensional python list - Stack Overflow
python - Write a 2-d array in a for-loop - Stack Overflow
python - Create 2D numpy array through nested loop - Stack Overflow
How to create 2d NumPy array with for loop [Python] - Stack Overflow
I have this two arrays
array1 = [1. 0.91 0.8 0.68 0.61 0.57 0.52 0.46 0.39 0.3 ]
array2 = [188.38188649 201.69718164 210.51306563 221.65195842 229.91542541 238.11787625 257.072488 ]
I need to multiple the second array by each value in the first one. I have tried using a for loop like this
for i in range (array1):
new_array = array2 * array1[i]But it shows me this error
TypeError: only integer scalar arrays can be converted to a scalar index
Any help is appreciated, thank you.
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]]
First lets focus on the append operation,
import numpy as np
a = np.array( [1,2,3] )
b = np.array( [4,5,6] )
np.append( a, b )
produces
array( [1, 2, 3, 4, 5, 6] )
What you might want is
np.append( [a], [b], 0 )
which produces
array([ [1, 2, 3], [4, 5, 6] ])
Note that here a 0 appears in the third parameter to specify the axis for the append operation.
Regarding the error, the index h, tells numpy to append to the 'h'-th element. That's probably not what you intended. Also, the index is not correct since 'h' is always one more than the number of elements in the array at the point where the call occurs.
I think you're trying to append a value to result_array. You should try without the (h) because what that will do is try to index the value already existing in result_array at index h. But since the array is empty to start so there is no 0th value, and h=0 for the first iteration, so that's why you're getting the error.
Maybe try np.append(result_array, inter2d_result)
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.
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(_)