0

I have two lists:

  • This one has two elements with strings with delamination ';'.
new_lst = [
    '50% Polyester;50% Polyester;50% Polyester;50% Polioster;50% Poliestere',
    '50% Elastane;50% Elasthanne;50% Elasthan;50% Elastano;50% Elastan'
]
  • The Second list only has one element with same deliminator ';'
newSpecialComp = [
    'Back:;Dos:;Rockseite:;Parte trasera:;Schiena:'
]

I need to combine the FIRST element from the first list new_lst with the first element of the second list newSpecialComp.

Desired output to be a list with two elements but first being the combination of the two above.:

Final_lst = [
    'Back: 50% Polyester;Dos: 50% Polyester;Rockseite: 50% Polyester;Parte trasera: 50% Polioster;Schiena: 50% Poliestere',
    '50% Elastane;50% Elasthanne;50% Elasthan;50% Elastano;50% Elastan'
]
for i, j in zip(newsetSpecialComp, new_lst):
        elem2 = ';'.join(map(lambda x:i+' '+x,j.split(';')))
        final_lst.append(elem2)
        print('Final_lst :', final_lst)

This is what i tried. The output is close but not ideal:

Final_lst = [
    'Back: 50% Polyester;Back: 50% Polyester;Back: 50% Polyester;Back: 50% Polioster;Back: 50% Poliestere', 
    'Dos: 50% Elastane;Dos: 50% Elasthanne;Dos: 50% Elasthan;Dos: 50% Elastano;Dos: 50% Elastan'
]

1 Answer 1

1

I think the notation you are missing to help you solve this is zip():

new_lst = [
    '50% Polyester;50% Polyester;50% Polyester;50% Polioster;50% Poliestere',
    '50% Elastane;50% Elasthanne;50% Elasthan;50% Elastano;50% Elastan'
]
newSpecialComp = [
    'Back:;Dos:;Rockseite:;Parte trasera:;Schiena:'
]

finalList = new_lst.copy()

# Assuming len(newSpecialComp) <= len(new_lst)
for index, val in enumerate(newSpecialComp):
    finalList[index] = ';'.join(' '.join(x) for x in zip(val.split(';'), new_lst[index].split(';')))

print(*finalList, sep='\n')

Output:

Back: 50% Polyester;Dos: 50% Polyester;Rockseite: 50% Polyester;Parte trasera: 50% Polioster;Schiena: 50% Poliestere
50% Elastane;50% Elasthanne;50% Elasthan;50% Elastano;50% Elastan

--- The more Verbose way to write it ---

new_lst = [
    '50% Polyester;50% Polyester;50% Polyester;50% Polioster;50% Poliestere',
    '50% Elastane;50% Elasthanne;50% Elasthan;50% Elastano;50% Elastan'
]
newSpecialComp = [
    'Back:;Dos:;Rockseite:;Parte trasera:;Schiena:'
]

finalList = new_lst.copy()

# Assuming len(newSpecialComp) <= len(new_lst)
for index, val in enumerate(newSpecialComp):
    str1 = val.split(';')
    str2 = new_lst[index].split(';')
    combined_strings = [' '.join(x) for x in zip(str1, str2)]
    finalList[index] = ';'.join(combined_strings)

print(*finalList, sep='\n')
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much it works. Could you give a brief explanation of what is happening your code?
Add prints to str1, str2, and combined_strings in the bottom code block. I could explain it but it would be shorter and probably better if you look at the values as the code runs. If you still don't understand it let me know.

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.