I was solving some problems at geeksforgeeks
and I came across a particluar question where the inputs are provided in the test case as shown:
2 2 # denotes row, column of the matrix
1 0 0 0 # all the elements of the matrix in a single line separated by a single space.
I am not getting how to initialize my 2D array with the inputs given in such a manner.
P.S. I can't use split as it will split all the elements on in a single array from which I have to read again each element. I am looking for more simple and pythonic way.
You should use
.split
. And you also need to convert the split string items toint
. But you can do that very compactly, if you want to:demo
If you get a SyntaxError on
mat = [*map(list, zip(*[data] * cols))]
change it toOr upgrade to a newer Python 3. ;)
After using split on both strings:
I'd got with: