printing a two dimensional array in python

后端 未结 7 1879
执念已碎
执念已碎 2020-12-01 02:39

I have to print 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            


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 03:30

    I used numpy to generate the array, but list of lists array should work similarly.

    import numpy as np
    def printArray(args):
        print "\t".join(args)
    
    n = 10
    
    Array = np.zeros(shape=(n,n)).astype('int')
    
    for row in Array:
        printArray([str(x) for x in row])
    

    If you want to only print certain indices:

    import numpy as np
    def printArray(args):
        print "\t".join(args)
    
    n = 10
    
    Array = np.zeros(shape=(n,n)).astype('int')
    
    i_indices = [1,2,3]
    j_indices = [2,3,4]
    
    for i in i_indices:printArray([str(Array[i][j]) for j in j_indices])
    

提交回复
热议问题