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
Any way, any help ?
[Python] generating a list of numers
[Python] generating a list of numers
mahmoud_shihab@hotmail.com
Re: [Python] generating a list of numers
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
Of course, this may not be the answer you're looking for, since i don't know the exact problem that you have...
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;
¯\_(ツ)_/¯ It works on my machine...
Re: [Python] generating a list of numers
yeah what I want is some thing slightly different , I don't want a random number. So instead of:
I'll write in python:
and the rest of the code will work fine
Thanks alot for the reply gogeta
Code: Select all
int a = random(0, 99999);
Code: Select all
for i in range (99999):
Thanks alot for the reply gogeta
mahmoud_shihab@hotmail.com
Re: [Python] generating a list of numers
And now, for some serious Python:
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:
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:
Then
Code: Select all
["%05d" % i for i in range(0,10000)]
Code: Select all
for i in range(0,10000):
print "%05d" % i
Actually, you can do it like this:
Code: Select all
def pad(x,n):
return (n-len(str(x)))*'0' + str(x)
Code: Select all
[pad(i,5) for i in range(0,10000)]
Re: [Python] generating a list of numers
@leetnigga: Very goog explanation to padding , but I would use this padding-method instead if writing my own:
Code: Select all
def pad(x,n):
return "%%0%dd" % n % x
Re: [Python] generating a list of numers
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).John_W wrote:@leetnigga: Very goog explanation to padding , but I would use this padding-method instead if writing my own:Code: Select all
def pad(x,n): return "%%0%dd" % n % x