get a list printed in hexadecimal notation
Bengt Richter
bokr at oz.net
Wed Jul 24 21:09:35 EDT 2002
On 24 Jul 2002 16:41:18 +0200, Andreas Kostyrka <andreas at kostyrka.priv.at> wrote:
>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']
>
Cool, but did I miss something? why the two for's? E.g., (single line optional ;-)
>>> hexlist=lambda l: [
... (isinstance(y,list) and hexlist(y)) or
... (isinstance(y,int) and hex(y)) or
... y
... for y in l]
>>> l = ('spam',10,15,'more spam',['nested',65535,'spam'],'spam',255)
>>> hexlist(l)
['spam', '0xa', '0xf', 'more spam', ['nested', '0xffff', 'spam'], 'spam', '0xff']
Regards,
Bengt Richter
More information about the Python-list
mailing list