I am assigning values to a nested matrix in a traditional for loop.
matrix= [[0 for j in range(3)]for i in range(3)]
value = 10
# Setting a value to a particular row in the matrix
for i in range(3):
if i == 2:
for j in range(3):
matrix[i][j] = 10
# Setting a value to a particular column in the matrix
val = 20
for i in range(3):
for j in range(3):
if j == 1:
matrix[i][j] = 20
Can assigning of the values to the matrix be done in a nested list comprehension? I did try this :
matrix = [[value for j in i if j == col ]for i in matrix]
But it isn't modifying the matrix, instead creates a new one.How can I accomplish this with nested list comprehensions?
matrix[:] = [[value for j in i if j == col ]for i in matrix]would change the original list but I don't think your examples are the samenumpy?