[Python] generating a list of numers

Questions about programming languages and debugging
Post Reply
User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

[Python] generating a list of numers

Post by moudy »

Hello Every one.
I want to generate numbers in a fashion such as:
- all the numbers have 5 digits
- first number is 00000
- second 00001 etc...
- when a number fills a new digit place, the zeros to the left are decreased (00123).
It sounds silly, but I'm not figuring out how to do it :oops:
Any way, any help ? :D
mahmoud_shihab@hotmail.com

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: [Python] generating a list of numers

Post by Gogeta70 »

Just generate a number between 0 and 99,999. Then check the length of the number and output the necessary number of 0's. For example:

Note: This is just pseudo code, not a real language

Code: Select all

int a = random(0, 99999);

if(a < 10)
  echo "0000" + a;
else if(a < 100)
  echo "000" + a;
else if(a < 1000)
  echo "00" + a;
else if (a < 10000)
  echo "0" + a";
else 
  echo a;

Of course, this may not be the answer you're looking for, since i don't know the exact problem that you have...
¯\_(ツ)_/¯ It works on my machine...

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: [Python] generating a list of numers

Post by moudy »

yeah what I want is some thing slightly different :P, I don't want a random number. So instead of:

Code: Select all

int a = random(0, 99999);
I'll write in python:

Code: Select all

for i in range (99999): 
and the rest of the code will work fine :)
Thanks alot for the reply gogeta
mahmoud_shihab@hotmail.com

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: [Python] generating a list of numers

Post by Gogeta70 »

No problem ^_^
¯\_(ツ)_/¯ It works on my machine...

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Re: [Python] generating a list of numers

Post by leetnigga »

And now, for some serious Python:

Code: Select all

["%05d" % i for i in range(0,10000)]
That'll give you a list of strings of all the numbers from 0 to 9999, padded with 5 zeroes. If you want to print it, you can loop through the list or do it an other way:

Code: Select all

for i in range(0,10000):
    print "%05d" % i
Number padding is solved in pretty much every high-level language with a sprintf-like function, like print format % arguments in Python above. If you want to do it manually, find an implementation of printf and copy that.

Actually, you can do it like this:

Code: Select all

def pad(x,n):
    return (n-len(str(x)))*'0' + str(x)
Then

Code: Select all

[pad(i,5) for i in range(0,10000)]

User avatar
John_W
forum buddy
forum buddy
Posts: 24
Joined: 02 May 2010, 16:00
13
Contact:

Re: [Python] generating a list of numers

Post by John_W »

@leetnigga: Very goog explanation to padding =D> , but I would use this padding-method instead if writing my own:

Code: Select all

def pad(x,n):
    return "%%0%dd" % n % x

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Re: [Python] generating a list of numers

Post by leetnigga »

John_W wrote:@leetnigga: Very goog explanation to padding =D> , but I would use this padding-method instead if writing my own:

Code: Select all

def pad(x,n):
    return "%%0%dd" % n % x
That's very clever. I wrote my pad() to explain how it might be implemented inside Python's or any other language's sprintf function (i.e without using the function itself).

User avatar
John_W
forum buddy
forum buddy
Posts: 24
Joined: 02 May 2010, 16:00
13
Contact:

Re: [Python] generating a list of numers

Post by John_W »

Oops :-99

Post Reply