[Tutor] three numbers for one

Jim Mooney cybervigilante at gmail.com
Mon Jun 10 04:51:34 CEST 2013


On 9 June 2013 08:22, Steven D'Aprano <steve at pearwood.info> wrote:

> On 10/06/13 00:26, Oscar Benjamin wrote:
>>
>> On 8 June 2013 06:49, eryksun <eryksun at gmail.com> wrote:

>>>>> def is_ascii_digit(string):
>>
>> ...     return not (set(string) - set('0123456789'))

Darn, here I thought sets were kind of useless, and some sources hint
as much so I filed them away as forgettable, but I  see now that they
can be a real time saver. I wrote a seven line module that tested raw
numbers or strings as simple ints, but using sets cuts it to two for
both numbers and numeric strings, and also checking False on the empty
string. I'll have to keep sets in mind.

def isdig(inp):  #py33
    inp = str(inp)
    return False if inp == '' else not (set(inp) - set('0123456789'))

-- 
Jim
Today is the day that would have been tomorrow if yesterday was today


More information about the Tutor mailing list