0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think the first answer works, I want a matrix to fill it with numbers, according to Levehnstein algorithm of two strings, in function of its similarities between characters. I think it will work isn't it?
@ZRTSTR Your question seems to be more about displaying the lists, rather than creating them... If you have written a specific function that will give the Levenhnstein output, I can try to incorporate it into this, but that wasn't really what you asked. As it stands, this answers your question, and you should really ask a new question about that algorithm for people to come help you on... Hope this works for you though, please accept if it does.
Your answer was helped me, don't worry about it, I will create another discussion.
@ZRTSTR Yes, that is the right thing to do! Please accept (click the green tick) thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.