简体   繁体   中英

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

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 attempt at doing so:

ogm = repr(ogm).replace(',', ' ')
ogm = repr(ogm).replace('[', ' ')
ogm = repr(ogm).replace("'", ' ')
ogm = repr(ogm).replace('"', ' ')
print repr(ogm).replace(']', ' ') 

This is the input:

[[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]]

This is the output:

"'    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    '"

I'm encountering two problems:

  1. There are stray " and ' which I can't get rid of
  2. I have no idea how to do a newline

Simple way:

for row in list2D:
    print " ".join(map(str,row))

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
print "\n".join(" ".join(map(str, line)) for line in ogm)

如果您希望行和列转置

print "\n".join(" ".join(map(str, line)) for line in zip(*ogm))

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
for row in list2D:
    print(*row)
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....... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM