Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating number format

Needs Regular Fix
 
Join Date: Feb 2007
Posts: 438
#1: Apr 24 '07
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

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: Apr 24 '07

re: Creating number format


Quote:

Originally Posted by psbasha

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']
Needs Regular Fix
 
Join Date: Feb 2007
Posts: 438
#3: Apr 24 '07

re: Creating number format


Thanks for the reply.
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,Ymin = 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
Needs Regular Fix
 
Join Date: Feb 2007
Posts: 438
#4: Apr 24 '07

re: Creating number format


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%0Indexd%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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#5: Apr 24 '07

re: Creating number format


Quote:

Originally Posted by psbasha

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%0Indexd%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. >>> 
Needs Regular Fix
 
Join Date: Feb 2007
Posts: 438
#6: Apr 24 '07

re: Creating number format


Quote:

Originally Posted by bvdet

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,

It washelpful
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#7: Apr 24 '07

re: Creating number format


Quote:

Originally Posted by psbasha

Thanks BV,

It washelpful

You are welcome. A generic function to generate number sequence strings is a bit more work than I have time for at the moment.
Reply