Creating number format | Needs Regular Fix | | Join Date: Feb 2007
Posts: 438
| | |
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
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,563
| | | 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: - def format_numbers(X,Y,Z,n,u):
-
x,y,z = X,Y,Z
-
outList = []
-
while x <= u:
-
while y <= u:
-
if n == 0:
-
return outList
-
else:
-
outList.append('%02d%02d%02d' % (x, y, z))
-
y += 1
-
n -= 1
-
y = Y
-
x += 1
-
return outList
-
-
print format_numbers(1,5,1,8,10)
-
-
>>> ['010501', '010601', '010701', '010801', '010901', '011001', '020501', '020601']
| | Needs Regular Fix | | Join Date: Feb 2007
Posts: 438
| | | 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
| | | 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
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,563
| | | 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: - >>> def plcs(n,p):
-
... n = str(n)
-
... return (p-len(n))*'0'+n
-
...
-
>>> '%s%s%s' % (plcs(5,4), plcs(6,4), plcs(8,4))
-
'000500060008'
-
>>>
| | Needs Regular Fix | | Join Date: Feb 2007
Posts: 438
| | | re: Creating number format Quote:
Originally Posted by bvdet You could do something like this: - >>> def plcs(n,p):
-
... n = str(n)
-
... return (p-len(n))*'0'+n
-
...
-
>>> '%s%s%s' % (plcs(5,4), plcs(6,4), plcs(8,4))
-
'000500060008'
-
>>>
Thanks BV,
It washelpful
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,563
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|