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
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? - Stack Overflow
python - Creating 2D arrays and input values in it with a while loop? - Stack Overflow
loops - Iterating over a 2 dimensional python list - Stack Overflow
Are 2D arrays memory-efficient in Python?
What are the challenges when working with 2D arrays?
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 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) /
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: "))
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.
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.