More "modern python" way of doing things.
[[ randint(0,4) for x in range(0,4)] for y in range(0,4)]
Its simply a pair of nested list comprehensions.
Answer from Shayne on Stack OverflowMore "modern python" way of doing things.
[[ randint(0,4) for x in range(0,4)] for y in range(0,4)]
Its simply a pair of nested list comprehensions.
The only time when you add 'rows' to the status array is before the outer for loop.
So - status[0] exists but status[1] does not.
you need to move status.append([]) to be inside the outer for loop and then it will create a new 'row' before you try to populate it.
There are fancier ways, but this is the most straightforward:
>>> rows = 3
>>> columns = 7
>>> n = 1
>>> matrix = []
>>> for _ in range(rows):
... sub = []
... for _ in range(columns):
... sub.append(n)
... n += 1
... matrix.append(sub)
...
>>> matrix
[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21]]
And for good measure, a fancy way:
>>> import itertools
>>> counter = itertools.count(1)
>>> rows = 3
>>> columns = 7
>>> matrix = [[n for n, _ in zip(counter, range(columns))] for _ in range(rows)]
>>> matrix
[[1, 2, 3, 4, 5, 6, 7], [9, 10, 11, 12, 13, 14, 15], [17, 18, 19, 20, 21, 22, 23]]
>>>
Use list comprehension. You want 3 rows so the base of the list comprehension is: [for y in range(rows)]. You want to have incrementing numbers starting with a number divisible by columns but starting from 1 so: range(columns*y+1,...) and you want to have columns range (7) so range(columns*y+1,columns+(columns*y+1)) and then turn that into a list.
rows=3
columns=7
matrix=[list(range(columns*y+1,columns+(columns*y+1))) for y in range(rows)]
print(matrix)
#outputs: [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21]]
I guess the easiest way to do this is to reshape an arange from 0 to 100:
>>> np.arange(100).reshape(10, -1)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
Here the .reshape(..) call will thus transform the matrix such that it is a 2D-array, with 10 "rows" and a number of columns such that the total amount of cells is 100.
In case you do not want to construct a 2D-array, but a Python list of 1D arrays, we can use list comprehension:
[np.arange(i, i+10) for i in range(0, 100, 10)]
A more pythonic way of doing your task would be one liner
import numpy as np
print(np.reshape(np.arange(0,100),(10,10)))
Are you necessarily looking to use for-loops? I am asking because this problem can be solved in simpler and more efficient ways such as:
import numpy as np
a_values = np.random.rand(20,402) #Store random values in a_values with a shape of 20 rows and 402 columns
avg_columns = a_values.mean(axis=0) #Calculate the mean of each column
print(avg_columns)
Documentation for generating random values: numpy.random.rand
EDIT:
I assumed that xa is randomized in each iteration (make sure to replace it with the xa that you are generating). You can initialize an empty a_values array, and add the generated xa array to it in each iteration using np.vstack. For the first iteration, when a_value is empty, I made it equal to xa (if a_values.size else xa).
import numpy as np
a_values = np.array([])
for i in range(20):
xa = np.random.rand(1,402)
a_values = np.vstack([a_values, xa]) if a_values.size else xa
avg_columns = a_values.mean(axis=0) #Average of each column
Documentation for vstack: numpy.vstack
I don't think you can change the shape of A_values after it's defined. So you should start with:
A_values = numpy.zeros([20,402])
And if your for loop is iterating through the rows, your index should be [i, 1] (numpy array indexes are not like cartesian plane coords).
But you want to change entire rows of A_values, to do this you use just the first index:
A_values[row] = some_row
Remember that "some_row" needs to have the proper lenght, otherwise you get a ValueError.
Your final code will look like this:
A_values = numpy.zeros([20, 402])
for i in range(0,20):
A_values([i]) = xa
PS: You should describe the errors you are getting and be clearer if you want help with the "xa" values as well...
First you have to install numpy using
$ pip install numpy
Then the following should work
import numpy as np
n = 100
matrix = np.zeros((n,2)) # Pre-allocate matrix
for i in range(1,n):
matrix[i,:] = [3*i, i**2]
A faster alternative:
col1 = np.arange(3,3*n,3)
col2 = np.arange(1,n)
matrix = np.hstack((col1.reshape(n-1,1), col2.reshape(n-1,1)))
Even faster, as Divakar suggested
I = np.arange(n)
matrix = np.column_stack((3*I, I**2))
This is very pythonic form to produce a list, which you can easily swap e.g. for np.array, set, generator etc.
n = 10
[[i*3, i**2] for i, i in zip(range(0,n), range(0,n))]
If you want to add another column it's no problem. Simply
[[i*3, i**2, i**(0.5)] for i, i in zip(range(0,n), range(0,n))]
The four sums you want can be calculated efficiently like this:
import numpy as np
arr = np.array(data)
w = arr[0::2,0] # array([ 1, 3, 5, 13])
x = arr[0::2,1] # array([ 7, 9, 11, 14])
y = arr[1::2,0] # array([ 2, 4, 6, 15])
z = arr[1::2,1] # array([ 8, 10, 12, 16])
B = [[w.sum(), x.sum()], [y.sum(), z.sum()]]
arr[1::2,0] means "Starting from row 1, take every second row, then take column 0."
def slice_array(raw_data):
ret = [[0,0],
[0,0]]
for d in range(len(raw_data)):
if d % 2 == 0:
ret [0][0] += raw_data[d][0]
ret [0][1] += raw_data[d][1]
if d % 2 == 1:
ret [1][0] += raw_data[d][0]
ret [1][1] += raw_data[d][1]
return ret
This should work. Just give the array you want to slice as input.
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) /
You should use numpy.zeros. If that isn't an option, you want the first version. In the second version, if you change one value, it will be changed elsewhere in the list -- e.g.:
>>> 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]]
This is because (as you read the expression from the inside out), you create a list of 10 zeros. You then create a list of 10 references to that initial list of 10 zeros.
Note that this will also work and it avoids the nested list comprehension:
zeros = [ [0]*M for _ in range(N) ]
If numpy isn't on the table, this is the form I would use.
(Use xrange if you're still stuck in the python2.x dark ages :).)
for Python 3 (no more xrange), the preferred answer
zeros = [ [0] * N for _ in range(M)]
for M x N array of zeros