Member Avatar for apeiron27

ok i'm not sure if i can use regex to do this it'd be great if you can give some suggestions!

i have a list of pure numbers in one hand
and another list of also numbers but with dashes.

it's like this:
list1 = [245366556346,4534525326562,56345254234237...
list2 = [75465-54224-4,3-55-342526,....

as you can see the postion of dash changes.
given that all my numbers have same length, how do I introduce dash to exactly the same way to pure number list?

what i mean is if I have 12345 in list 1 and 76-543 in list 2, i want to create a list that has 12-345.

Here is a possible solution without regexes

def add_dash(str1, str2):
    src = iter(str2)
    return "".join('-' if x == '-' else next(src) for x in str1)

s1 = "75465-54224-4"
s2 = "245366556346"

print add_dash(s1, s2)

"""my output -->
24536-65563-4
"""

Notice that the 6 at the end of s2 was lost because s1 has 11 digits and s2 has 12 digits, which contradicts your statement that all numbers have the same length.

Here alternative without iterator, which leaves extra digits:

def add_dash(control, to_add):
    as_list = list(to_add)
    p = 0
    while '-' in control[p:]:
        p = control.find('-', p) + 1
        as_list.insert(p - 1, '-')
    return ''.join(as_list)


s1 = "75465-54224-4"
s2 = "245366556346"

print add_dash(s1, s2)

"""my output -->
24536-65563-46
"""

You can leave extra digits with generators too

from itertools import chain

def add_dash(str1, str2):
    src = iter(str2)
    return "".join(chain(('-' if x == '-' else next(src) for x in str1), src))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.