printing a two dimensional array in python

后端 未结 7 1858
执念已碎
执念已碎 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:28
    for i in A:
        print('\t'.join(map(str, i)))
    
    0 讨论(0)
  • 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])
    
    0 讨论(0)
  • 2020-12-01 03:31

    There is always the easy way.

    import numpy as np
    print(np.matrix(A))
    
    0 讨论(0)
  • 2020-12-01 03:33

    In addition to the simple print answer, you can actually customise the print output through the use of the numpy.set_printoptions function.

    Prerequisites:

    >>> import numpy as np
    >>> inf = np.float('inf')
    >>> A = np.array([[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]])
    

    The following option:

    >>> np.set_printoptions(infstr="(infinity)")
    

    Results in:

    >>> print(A)
    [[        0.         1.         4. (infinity)         3.]
     [        1.         0.         2. (infinity)         4.]
     [        4.         2.         0.         1.         5.]
     [(infinity) (infinity)         1.         0.         3.]
     [        3.         4.         5.         3.         0.]]
    

    The following option:

    >>> np.set_printoptions(formatter={'float': "\t{: 0.0f}\t".format})
    

    Results in:

    >>> print(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  ]]
    
    
    

    If you just want to have a specific string output for a specific array, the function numpy.array2string is also available.

    0 讨论(0)
  • 2020-12-01 03:36
    print(mat.__str__())
    

    where mat is variable refering to your matrix object

    0 讨论(0)
  • 2020-12-01 03:40

    using indices, for loops and formatting:

    import numpy as np
    
    def printMatrix(a):
       print "Matrix["+("%d" %a.shape[0])+"]["+("%d" %a.shape[1])+"]"
       rows = a.shape[0]
       cols = a.shape[1]
       for i in range(0,rows):
          for j in range(0,cols):
             print "%6.f" %a[i,j],
          print
       print      
    
    
    def printMatrixE(a):
       print "Matrix["+("%d" %a.shape[0])+"]["+("%d" %a.shape[1])+"]"
       rows = a.shape[0]
       cols = a.shape[1]
       for i in range(0,rows):
          for j in range(0,cols):
             print("%6.3f" %a[i,j]),
          print
       print      
    
    
    inf = float('inf')
    A = np.array( [[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]])
    
    printMatrix(A)    
    printMatrixE(A)    
    

    which yields the output:

    Matrix[5][5]
         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
    
    Matrix[5][5]
     0.000  1.000  4.000    inf  3.000
     1.000  0.000  2.000    inf  4.000
     4.000  2.000  0.000  1.000  5.000
       inf    inf  1.000  0.000  3.000
     3.000  4.000  5.000  3.000  0.000
    
    0 讨论(0)
提交回复
热议问题