Sunday, March 04, 2018

Python : Pretty Print a Table

That is, if you've set up a board
[ ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'] ]

How do you get

O O O O O
..
O O O O O

You get the idea..

def print_board(board_in ) :
  for row in board_in :
    print " ".join(row)

Is one way out..

Per https://stackoverflow.com/questions/13214809/pretty-print-2d-python-list

these may also be of help :

from prettytable import PrettyTable

x = [["A", "B"], ["C", "D"]]

p = PrettyTable()
for row in x:
    p.add_row(row)

print p.get_string(header=False, border=False)

from pandas import *
x = [["A", "B"], ["C", "D"]]
print DataFrame(x)

   0  1
0  A  B
1  C  D
Python pretty print a dict :

pprint.pprint( dict ) will give you :
>>> pprint.pprint( transition_counts )
{('NN', 'NN'): 16241,
 ('NN', 'RB'): 2431,
 ('NN', 'TO'): 5256,
 ('RB', 'NN'): 358,
 ('RB', 'RB'): 2263,
 ('RB', 'TO'): 855,
 ('TO', 'NN'): 734,
 ('TO', 'RB'): 200,
 ('TO', 'TO'): 2}

No comments: