Number combinations

Bruno Desthuilliers onurb at xiludom.gro
Wed Jul 19 10:19:19 EDT 2006


placid wrote:
> Hi all,
> 
> Just wondering if there is a better way of generating a 4 digit number
> (that gets converted to a string), ive got the following code which
> generates strings between 0000-9999.
> 
> <code>
> 
> for a in range(0,10):
>     for b in range(0,10):
>         for c in range(0,10):
>             for d in range(0,10):

You could reuse the same range...

>                 print "%s%s%s%s" %(str(a), str(b), str(c),str(d)

And there's no need to convert to string here.

def parrot():
    r = range(10)
    return ["%s%s%s%s" % (a, b, c, d) \
             for a in r \
             for b in r \
             for c in r \
             for d in r]

But there's certainly better solutions...

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list