473,857 Members | 2,173 Online

Creating number format

440 Contributor
I have a input format XX:YY:ZZ and the start value may be 'n' digit numbers for X,Y,Z.

So I have to assign the numbers based on the above format ( Format may change)

Say

Example - 1

Inputs :

Number Format :- XX:YY:ZZ

Start Values for
X = 1
Y = 5
z = 1

I have to create 5 numbers with +1 increment for 'Y'direction value

Here 'Z' will be constant

XX:YY:ZZ

010501
010601
010701
010801
010901
011001

Example - 2
------------------
Number Format :- X:YY:Z
X = 1
Y = 5
z = 1

I have to create the numbers with +1 increment for 'X' & 'Y'direction value

Here 'Z' will be constant
X:YY:Z

010501
010601
010701
010801
010901
011001
020501
020601
020701
020801
020901
021001

How to arrive at these numbers based on the format and the start numbers?

Thanks
PSB
Apr 24 '07 #1
6 2922
bvdet
2,851 Recognized Expert Moderator Specialist
I have a input format XX:YY:ZZ and the start value may be 'n' digit numbers for X,Y,Z.

So I have to assign the numbers based on the above format ( Format may change)

Say

Example - 1

Inputs :

Number Format :- XX:YY:ZZ

Start Values for
X = 1
Y = 5
z = 1

I have to create 5 numbers with +1 increment for 'Y'direction value

Here 'Z' will be constant

XX:YY:ZZ

010501
010601
010701
010801
010901
011001

Example - 2
------------------
Number Format :- X:YY:Z
X = 1
Y = 5
z = 1

I have to create the numbers with +1 increment for 'X' & 'Y'direction value

Here 'Z' will be constant
X:YY:Z

010501
010601
010701
010801
010901
011001
020501
020601
020701
020801
020901
021001

How to arrive at these numbers based on the format and the start numbers?

Thanks
PSB
This is not ideal, but it seems to meet your requirements:
Expand|Select|Wrap|Line Numbers
1. def format_numbers(X,Y,Z,n,u):
2.     x,y,z = X,Y,Z
3.     outList = []
4.     while x <= u:
5.         while y <= u:
6.             if n == 0:
7.                 return outList
8.             else:
9.                 outList.append('%02d%02d%02d' % (x, y, z))
10.                 y += 1
11.                 n -= 1
12.         y = Y
13.         x += 1
14.     return outList
15.
16. print format_numbers(1,5,1,8,10)
17.
18. >>> ['010501', '010601', '010701', '010801', '010901', '011001', '020501', '020601']
Apr 24 '07 #2
psbasha
440 Contributor
But the number format will be changing

Say:

a) Format :- X:Y:Z

Then the values will be 111 to 991 ( Z = 1 Constant,Xmin,Y min = 1 and Xmax,Ymax = 9)

b) Format :- XX:YY:ZZ

Then the values will be

010101 to 999901 ( Z = 01 Constant, Xmin,Ymin = 01 and Xmax,Ymax = 99)

c) Format :- XX:Y:ZZ
Then the values will be

01101 to 99901 ( Z = 01 Constant, Xmin =01,Ymin = 1 and Xmax =99,Ymax = 9)

d) Format :- XX:Y:ZZ
Then the values will be

01101 to 99199 ( Y = 1 Constant, Xmin =01,Zmin =0 1 and Xmax =99,Zmax = 99)

and soon.

Is it possible to create the above formating in amore generic way.

Input will be format and the start index of X,Y,Z (But one index will be constant,out of three)

-PSB

-PSB
Apr 24 '07 #3
psbasha
440 Contributor
Instead of giving constant value (say 4 in this case),is it possible to give variable name as shown below and get the solution

[code]
Existing:
>>> outList =[]
>>> outList.append( '%04d%04d%04d' % (100, 51, 1))
>>> [ '010000510001']

Required:
Index will be varying based on the format

>>> Index = 2
>>> outList =[]
>>> outList.append( '%0Indexd%0Inde xd%0Indexd' % (100, 51, 1))
>>> outList
['100d51d1d']

But I am getting the expected output.May be my syntax is wrong.Correct me for the format

-PSB
Apr 24 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Instead of giving constant value (say 4 in this case),is it possible to give variable name as shown below and get the solution

Existing:
>>> outList =[]
>>> outList.append( '%04d%04d%04d' % (100, 51, 1))
>>> [ '010000510001']

Required:
Index will be varying based on the format

>>> Index = 2
>>> outList =[]
>>> outList.append( '%0Indexd%0Inde xd%0Indexd' % (100, 51, 1))
>>> outList
['100d51d1d']

But I am getting the expected output.May be my syntax is wrong.Correct me for the format

-PSB
You could do something like this:
Expand|Select|Wrap|Line Numbers
1. >>> def plcs(n,p):
2. ...     n = str(n)
3. ...     return (p-len(n))*'0'+n
4. ...
5. >>> '%s%s%s' % (plcs(5,4), plcs(6,4), plcs(8,4))
6. '000500060008'
7. >>>
Apr 24 '07 #5
psbasha
440 Contributor
You could do something like this:
Expand|Select|Wrap|Line Numbers
1. >>> def plcs(n,p):
2. ...     n = str(n)
3. ...     return (p-len(n))*'0'+n
4. ...
5. >>> '%s%s%s' % (plcs(5,4), plcs(6,4), plcs(8,4))
6. '000500060008'
7. >>>
Thanks BV,

Apr 24 '07 #6
bvdet
2,851 Recognized Expert Moderator Specialist
Thanks BV,