打印具有对齐列的列表系列

时间:2015-08-04 13:54:02

标签: python text printing format pprint

我有一个传递一个int列表和所需行长度的函数。假设数据的长度可以被行长度整除。列表中的所有整数都表示字节,因此它们不超过3位数,并且不小于0.这是我的函数:

    def pprint(data, rowlen):
    # Prints out the list in a nice grid format
    print '-' * 50
    for index in range(0, len(data), rowlen):
        print data[index:(index+rowlen)]
    print '-' * 50
    return data

输出看起来像这样:

[0, 0, 2, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0, 0]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0, 0, 0, 0, 0, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0]

正如你所看到的,这种作品,但我想要更像这样的东西:

[0  , 0  , 2  , 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0  , 0  ]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0  , 0  , 0  , 0  , 0  , 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139]
[139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0  ]

这样,所有数据都以网格格式对齐,很容易看到一切都在哪里。

是否有一种简单的方法可以实现这一点,而无需迭代每个条目并插入空格。

1 个答案:

答案 0 :(得分:2)

使用string.format

    data = [i for i in range(0, 200)]

    def pprint(data, rowlen):
        print '-' * 50
        for index in range(0, len(data), rowlen):
            print ['{:3}'.format(i) for i in data[index:(index+rowlen)]]
        print '-' * 50
        return data

    pprint(data, 10)