Number combinations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • placid

    Number combinations

    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):
    print "%s%s%s%s" %(str(a), str(b), str(c),str(d)

    </code>


    --Cheers



  • Tim Chase

    #2
    Re: Number combinations

    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):
    print "%s%s%s%s" %(str(a), str(b), str(c),str(d)
    >
    </code>
    Is there something wrong with

    for i in xrange(0,10000) : print "%04i" % i

    If you need the individual digits for something, you can use

    for i in xrange(0,10000) :
    d1,d2,d3,d4 = list("%04i" % i)
    # do something with d1,d2,d3,d4

    somewhat indelicate, but it works for me. :)

    -tkc




    Comment

    • Kevin Watters

      #3
      Re: Number combinations

      How about

      print ["%04d" % x for x in xrange(10000)]

      Comment

      • Bruno Desthuilliers

        #4
        Re: Number combinations

        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@xiludom. gro'.split('@')])"

        Comment

        • Alex Martelli

          #5
          Re: Number combinations

          placid <Bulkan@gmail.c omwrote:
          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):
          print "%s%s%s%s" %(str(a), str(b), str(c),str(d)
          for n in xrange(10000):
          print "%4.4d" % n


          Alex

          Comment

          • Boris Borcic

            #6
            Re: Number combinations

            Tim Chase wrote:
            ....
            If you need the individual digits for something, you can use
            >
            for i in xrange(0,10000) :
            d1,d2,d3,d4 = list("%04i" % i)
            strings are sequences too, and you need only write

            d1,d2,d3,d4 = "%04i" % i

            Comment

            Working...