Reputation:
I created this one,
n,m = 3, 3
Matrix = [[0 for x in range(n)] for y in range(m)]
print (Matrix)
But I want the matrix to be more visual like a current one, every "list" behind each other
[2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10, 11]
[7, 8, 9, 10, 11, 12]
I'd like to use iterations like "for" to create it because it will be more easy to solve my problem.
Upvotes: 0
Views: 358
Reputation: 20414
You can create a function
to display it:
def display(m):
for r in m:
print(r)
So printing Matrix
would normally result in:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
but, if instead you do display(Matrix)
after defining this function
somewhere in your code, you will get:
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
where each row
in the matrix
is beneath each other.
And of course, if you didn't want to define a matrix
populated with 0s
, you can just hard code the contents:
Matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
which to show the function
works, outputs the following from display(Matrix)
:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Note that it is probably better to call this a 2-dimensional
list
rather than a Matrix
to avoid confusion.
Upvotes: 3