Python pprint(): Can I format items in an array?
سؤال
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'
)?
المحلول
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.
لا تنتمي إلى StackOverflow