0

I have a two dimensional array of numbers, something like

[[1, 123, 2], [22, 4567, 33], [0, 0, 0]]

That I would like to print in a debugging session. It would be useful for the columns to line up.

Is there a way to tell pprint to use a particular printing format for the numbers (e.g. '%4d')?

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • possible duplicate of [How to extend pretty print module to tables?](http://stackoverflow.com/questions/3319540/how-to-extend-pretty-print-module-to-tables) – Acorn Apr 07 '12 at 22:58
  • @Acorn, the questions aren't similar at all. The other question wishes to format the lists. I'm fine with the list formatting, but want to apply additional formatting to the list elements. Normally I would use numpy for everything associated with 2-d arrays, but I need something that works in the standard python distribution, as I'm using this for homwork in a udacity class. – Mark Harrison Apr 07 '12 at 23:48

1 Answers1

1

If you aren't set on pprint then,

>>> masterList = [[1, 123, 2], [22, 4567, 33], [0, 0, 0]]
>>> print "\n".join("\t".join(["{0:04d}".format(num) for num in subList]) for subList in masterList)
0001    0123    0002
0022    4567    0033
0000    0000    0000
>>> 

otherwise refer to Acorn's comment.

John
  • 13,197
  • 7
  • 51
  • 101