How to format a string from an array?

Gerard Flanagan grflanagan at yahoo.co.uk
Thu Jun 14 03:06:30 EDT 2007


On Jun 13, 11:11 am, Allen <pruyu.c... at gmail.com> wrote:
> a = range(256)
> I want to output the formated string to be:
> 00 01 02 03 04 05 06 07   08 09 0a 0b 0c 0d 0e 0f
> 10 11 12 13 14 15 16 17   18 19 1a 1b 1c 1d 1e 1f
> ........................................................................
> f0 f1 f2 f3 f4 f5 6 f7   f8 f9 fa fb fc fd fe ff
>
> How to do it?

a = range(256)

import itertools as it

for k, g in it.groupby(a, lambda x: x//16):
    print ' '.join('%02x' % n for n in g)




More information about the Python-list mailing list