Display matrix in python

Asked

Viewed 480 times

-2

I have a matrix of n by n and use this part of the code to present it:

for i in range(len(data)):
    for j in range(len(data[0])):
        print ((data[i][j]))

Being data the matrix. But the output looks like this:

 5
 4
 1
10
 2
 3
 0
 2
 1
...

The values of data are coming from here

    data = [ [-1 for i in range(vertice)] for j in range(vertice)]
    for i in range(vertice):
        for j in range(vertice):
            if data[i][j] < 0:
                data[i][j] = random.randint(0, vertice)
                data[j][i] = random.randint(0, vertice)
            if i == j:
                data[i][i] = 0

    return data

I’d like to present it this way:

5 4 1 10 2
3 0 2  1 7 
3 0 2  1 7 
3 0 2  1 7 

I’ve tried a thousand and one ways, but I’m not getting there. What I’m doing wrong?

  • And what values do you have in data?

  • attribute Random values. It always shows, so I think it is this part of the code the error

  • And how do we know what’s wrong? For me it’s all right given past information. There’s random data being shown right.

  • it is the print that is wrong, but I put what you want

  • And how the print output should be?

  • Yes, it should be said. Sry.

  • in the form of a matrix, I identify at least

Show 2 more comments

2 answers

2


First, never go through a Python list with for i in range(len(data)) and then do data[i]. That’s not idiomatic (pythonic). You can just make one for linha in data.

data = [
    [5, 4, 1, 10, 2],
    [3, 0, 2,  1, 7],
    [3, 0, 2,  1, 7],
    [3, 0, 2,  1, 7]
]

for linha in data:
    for numero in linha:
        print(numero, end=" ")
    print()

The problem is that because 10 has more characters than others, the output will be:

5 4 1 10 2 
3 0 2 1 7 
3 0 2 1 7 
3 0 2 1 7 

You can get around this by formatting the output:

for linha in data:
    for numero in linha:
        print(f'{numero:>3}', end=" ")
    print()

Realize that instead of just showing off numero i exhibited {numero:>3}. This will always display 3 characters and the number will be aligned on the right:

  5   4   1  10   2 
  3   0   2   1   7 
  3   0   2   1   7 
  3   0   2   1   7 

Solves the problem for the 10, but what if in the matrix there is the number 9999? This I leave to you to solve.

1

From what I understand you want to display the matrix in a way that is more friendly, separating into rows into columns, you can do it as follows:

for i in range(len(data)):
    for j in range(len(data[0])):
       print ((data[i][j]), end=" ")
    print()

the Python by default, adds a line break at the end of the print, so for the lines the parameter end to assign a space, instead and a line break, already, the other print has the function of breaking the line whenever a matrix line is displayed fully.

Exit:

0 1 2 3 4 
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 
  • That’s right. Thank you Junior Nascimento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.