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])
How can I create multiple arrays using a for loop or to convert into a 2D array
python - Creating 2D arrays and input values in it with a while loop? - Stack Overflow
How to create 2d NumPy array with for loop [Python] - Stack Overflow
loops - Iterating over a 2 dimensional python list - 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.
To go over a 2D array you have to use two nested loops.
i = 0
j = 0
while (i < len(array)):
while(j < len(array[i]):
array[i][j] = value
j = j + 1
j = 0
i = i + 1
Those loops set in a 2D array each time value. value can be in this case whatever you want it to be. The first index determines the array in the array and the second index selects the value in the nested array. In your case when you have [[1, 2, 3], [4, 5, 6], [7, 8, 9]], array[1][2] == 6
This also works for arrays that are not symmetric.
Oups! Lists are mutable objects in Python, and a list contains reference to objects. So when you write array = [[0]* days]*city, the external list contains city pointers to an unique list initialized with days 0.
Demo:
>>> array = [[0]* 3]*3
>>> array[0][0] = 1
>>> array
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
You should write instead array = [ [0] * 3 for i in range(3)]
Demo:
>>> array = [ [0] * 3 for i in range(3)]
>>> array[0][0] = 1
>>> array
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
For your question just cascade loops:
for c in range(city):
for day in range(days):
array[c][day] = int(input("temperature: "))
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)
You're technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this "list comprehension".
# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)]
#You can now add items to the list:
Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range...
Matrix[0][6] = 3 # valid
Note that the matrix is "y" address major, in other words, the "y index" comes before the "x index".
print Matrix[0][0] # prints 1
x, y = 0, 6
print Matrix[x][y] # prints 3; be careful with indexing!
Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use "x" for both the inner and outer lists, and want a non-square Matrix.
If you really want a matrix, you might be better off using numpy. Matrix operations in numpy most often use an array type with two dimensions. There are many ways to create a new array; one of the most useful is the zeros function, which takes a shape parameter and returns an array of the given shape, with the values initialized to zero:
>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
Here are some other ways to create 2-d arrays and matrices (with output removed for compactness):
numpy.arange(25).reshape((5, 5)) # create a 1-d range and reshape
numpy.array(range(25)).reshape((5, 5)) # pass a Python range and reshape
numpy.array([5] * 25).reshape((5, 5)) # pass a Python list and reshape
numpy.empty((5, 5)) # allocate, but don't initialize
numpy.ones((5, 5)) # initialize with ones
numpy provides a matrix type as well, but it is no longer recommended for any use, and may be removed from numpy in the future.
To initialize a two-dimensional list in Python, use
t = [ [0]*3 for i in range(3)]
But don't use [[v]*n]*n, it is a trap!
>>> a = [[0]*3]*3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][0]=1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
A pattern that often came up in Python was
bar = []
for item in some_iterable:
bar.append(SOME EXPRESSION)
which helped motivate the introduction of list comprehensions, which convert that snippet to
bar = [SOME_EXPRESSION for item in some_iterable]
which is shorter and sometimes clearer. Usually, you get in the habit of recognizing these and often replacing loops with comprehensions.
Your code follows this pattern twice
twod_list = [] \
for i in range (0, 10): \
new = [] \ can be replaced } this too
for j in range (0, 10): } with a list /
new.append(foo) / comprehension /
twod_list.append(new) /