0

I am trying to code a program that will insert specific numbers before parts of an input, for example given the input "171819-202122-232425" I would like it to split up the number into pieces and use the dash as a delimiter. I have split up the number using list(str(input)) but have no idea how to insert the appropriate numbers. It has to work for any number Thanks for the help.

Output =

     (number)17
     (number)18                
     (number)19
     (number+1)20
     (number+1)21
     (number+1)22
     (number+2)23
     (number+2)24
     (number+2)25
1
  • Add a number every 2 digits in the input? That's what you want? Commented Dec 14, 2013 at 3:30

2 Answers 2

1

You could use split and regexps to dig out lists of your numbers:

Code

import re

mynum = "171819-202122-232425"
start_number = 5
groups = mynum.split('-') # list of numbers separated by "-"
number_of_groups = xrange(start_number , start_number + len(groups))

for (i, number_group) in zip(number_of_groups, groups):
    numbers = re.findall("\d{2}", number_group) # return list of two-digit numbers
    for x in numbers:
            print "(%s)%s" % (i, x)

Result

(5)17
(5)18
(5)19
(6)20
(6)21
(6)22
(7)23
(7)24
(7)25
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Code:

mInput = "171819-202122-232425"
number = 9 # Just an example
result = ""

i = 0
for n in mInput:
    if n == '-': # To handle dash case
        number += 1
        continue
    i += 1
    if i % 2 == 1: # Each two digits
        result += "\n(" + str(number) + ")" 
    result += n # Add current digit

print result

Output:

(9)17
(9)18
(9)19
(10)20
(10)21
(10)22
(11)23
(11)24
(11)25

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.