Writing a string.ishex function

Arnaud Delobelle arnodel at googlemail.com
Thu Jan 14 13:19:54 EST 2010


"D'Arcy J.M. Cain" <darcy at druid.net> writes:

> On Thu, 14 Jan 2010 09:07:47 -0800
> Chris Rebert <clp2 at rebertia.com> wrote:
>> Even more succinctly:
>> 
>> def ishex(s):
>>     return all(c in string.hexdigits for c in s)
>
> I'll see your two-liner and raise you.  :-)
>
> ishex = lambda s: all(c in string.hexdigits for c in s)

I'see your one-liner and raise you by four characters :o)

ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
ishex2 = lambda s: not(set(s)-set(string.hexdigits))     # Mine

Notice that ishex2 is more accurate than ishex1.  E.g.

>>> ishex1(['abc', '123'])
True
>>> ishex2(['abc', '123'])
False

-- 
Arnaud



More information about the Python-list mailing list