get a list printed in hexadecimal notation

Andreas Kostyrka andreas at kostyrka.priv.at
Wed Jul 24 10:41:18 EDT 2002


Am Fre, 2002-07-19 um 08.19 schrieb Oliver Eichler:
> Bengt Richter wrote:
> 
> >  >>> l = ('spam',10,15,'more spam',['nested',65535,'spam'],'spam',255)
> >          ^                                   ^^^^^                   ^
> >  >>> print  [isinstance(x,int) and hex(x) or x for x in l]
> >  ['spam', '0xa', '0xf', 'more spam', ['nested', 65535, 'spam'], 'spam',
> >  ['0xff']
> 
> I never do such complicated things at home ;) But you are right for more 
> complex lists the one liner won't fit.
Well it's not efficient, but:
hexlist=lambda l: [(isinstance(y,list) and hexlist(y)) or y for y in
[isinstance(x,int) and hex(x) or x for x in l]]
l = ('spam',10,15,'more spam',['nested',65535,'spam'],'spam',255)
print hexlist(l)

gives:
['spam', '0xa', '0xf', 'more spam', ['nested', '0xffff', 'spam'],
'spam', '0xff']

Andreas





More information about the Python-list mailing list