Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print series of lists with aligned columns

I have a function that is passed a list of ints, and the desired row length. It is assumed that the length of the data is evenly divisible by the row length. All the ints in the list represent bytes, so they are no longer than 3 digits, and no smaller than 0. Here is my function:

    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

The output looks something like this:

[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]

As you can see, this sort of works, but I'd like something more like this:

[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  ]

That way, all the data is aligned in a grid format and it is easy to see where everything is.

Is there an easy way to achieve this without having to iterate through every entry and inserting spaces.

like image 943
watersnake Avatar asked Dec 11 '25 10:12

watersnake


1 Answers

Use 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)
like image 76
Dima Kudosh Avatar answered Dec 13 '25 00:12

Dima Kudosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!