Python - best way to set a column in a 2d array to a specific value
multidimensional-array, python
Solution
A better solution would be:
data = [[0] * cols for i in range(rows)]
For the values of `cols = 2`, `rows = 3` we'd get:
data = [[0, 0],
[0, 0],
[0, 0]]
Then you can access it as:
v = data[row][col]
Which leads to:
val = 10
set_col = 5
for row in range(rows):
data[row][set_col] = val
Or the more Pythonic (thanks J.F. Sebastian):
for row in data:
row[set_col] = val
Problem
I have a 2d array, I would like to set a column to a particular value, my code is below. Is this the best way in python? ``` rows = 5 cols = 10 data = (rows * cols) *[0] val = 10 set_col = 5 for row in range(rows): data[row * cols + set_col - 1] = val ``` If I want to set a number of columns to a particular value , how could I extend this I would like to use the python standard library only Thanks