Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I need to display this python code in a 5x5 array the array should look like this :

0 1 4 (infinity) 3

1 0 2 (infinity) 4

4 2 0  1         5

(inf)(inf) 1 0   3

3 4 5   3        0

Can anyone help me print this table with the help of indices?

for k in range(n):

        for i in range(n):

            for j in range(n):

                if A[i][k]+A[k][j]<A[

1 Answer

0 votes
by (108k points)

There is a combination of list comprehensions and str joins that will help you to get your desired output:

inf = float('inf')

A = [[0,1,4,inf,3],

     [1,0,2,inf,4],

     [4,2,0,1,5],

     [inf,inf,1,0,3],

     [3,4,5,3,0]]

print('\n'.join([''.join(['{:4}'.format(item) for item in row]) 

      for row in A]))

The yields:

   0   1   4 inf   3

   1   0   2 inf   4

   4   2   0   1   5

 inf inf   1   0   3

   3   4   5   3   0

If you want to learn python then do check out the below python tutorial video for better understanding:

 

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...