Displaying python 2d list without commas, brackets, etc. and newline after every row

前端 未结 5 1832
Happy的楠姐
Happy的楠姐 2020-12-12 00:51

I\'m trying to display a python 2D list without the commas, brackets, etc., and I\'d like to display a new line after every \'row\' of the list is over.

This is my

相关标签:
5条回答
  • 2020-12-12 01:10

    Maybe join is appropriate for you:

    print "\n".join(" ".join(str(el) for el in row) for row in ogm)
    
    0 0 0 0 0 0 0 0 0 0
    1 1 0 1 0 0 0 0 0 0
    1 1 1 0 0 0 0 0 0 0
    0 1 1 0 0 0 0 0 1 1
    0 0 0 0 0 0 1 1 1 0
    0 0 0 1 1 0 1 1 1 1
    0 0 1 1 0 0 1 1 1 1
    0 1 0 0 0 0 0 1 1 0
    0 0 0 0 0 0 1 1 0 0
    1 0 1 1 1 1 0 0 0 0
    
    0 讨论(0)
  • 2020-12-12 01:10
    ogm = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 1, 0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0, 0, 0]]
    
    s1 = str(ogm)
    
    s2 = s1.replace('], [','\n')
    
    s3 = s2.replace('[','')
    
    s4 = s3.replace(']','')
    
    s5= s4.replace(',','')
    
    print s5
    

    btw the " is actually two ' without any gap

    i am learning python for a week. u guys have given some xcellent solutions. here is how i did it....this works too....... :)

    0 讨论(0)
  • 2020-12-12 01:19

    Simple way:

    for row in list2D:
        print " ".join(map(str,row))
    
    0 讨论(0)
  • 2020-12-12 01:19

    To make the display even more readable you can use tabs or fill the cells with spaces to align the columns.

    def printMatrix(matrix):
        for lst in matrice:
            for element in lst:
                print(element, end="\t")
            print("")
    

    It will display

    6       8       99
    999     7       99
    3       7       99
    

    instead of

    6 8 99
    999 7 99
    3 7 99
    
    0 讨论(0)
  • 2020-12-12 01:22
    print "\n".join(" ".join(map(str, line)) for line in ogm)
    

    If you want the rows and columns transposed

    print "\n".join(" ".join(map(str, line)) for line in zip(*ogm))
    
    0 讨论(0)
提交回复
热议问题