I am trying to create a matrix and then print it using python with the following expected output:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
The code follows:
matrix=[[]]
matrix = [[0 for x in range(4)] for x in range(4)]
print matrix
But the output comes as:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Please tell me why I'm getting this type of output and help with the correct one
What you are getting is the correct representation of a matrix. You have created a list that contains 4 lists, each list containing 4 '0's. If you want to print it differently then you have some options, but what you are printing is the representation of the above mentioned data structure.
for row in matrix:
print ' '.join(map(str,row))
this should work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With