Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个二维数字数组,比如
[[1, 123, 2], [22, 4567, 33], [0, 0, 0]]
我想在调试会话中打印。列对齐会很有用。
有没有办法告诉pprint对数字使用特定的打印格式(例如'%4d')?
pprint
'%4d'
如果你没有设置 pprint 那么,
>>> 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 >>>
否则请参阅 Acorn 的评论。