Home > Mobile >  How can I create a function that searches through two lists, adding a placeholder like ' '
How can I create a function that searches through two lists, adding a placeholder like ' '

Time:04-23

import re

list1 = ["I'm driving your car with you sleeping in the seat next to me", 'Like a baby, you twist and you turn', "You're travelling fast like a bird in a dream", 'Look at it go, look at it dance over the sky like a rocket', 'A love machine, a cinematic dream', 'So pure and it hurts when the beauty is lost in the speed', "'Cause everything matters to me", '(To me, to me, to me, to me)', 'To me', "You're a part of the dawn where the light comes from the dark", "You're a part of the morning and everything matters", 'And we are an atom and a star', "You're a part of the movement and everything matters", '(To me, to me, to me, to me)', "I'm watching your storm turn into form", 'And the clouds of the world like a burst', 'It dances and it twirls', 'On top of the world, it is good and it hurts', 'Look at it go, look at it dance over the sky like a rocket', 'A teacher, a simulated dream', 'A cure, a cure for the hurt', 'And the pleasure you feel is real', "You're a part of the dawn where the light comes from the dark", "You're a part of the morning and everything matters", 'And we are an atom and a star', "You're a part of the movement and everything matters", '(To me, to me, to me, to me)', '(To me, to me, to me, to me, to me)', "Quelque part, avant l'aube", 'Quand la lumière veut nous voir', "Quelque part dans le monde un oiseau s'endort", 'Sans bruit, toi et moi', 'Dans la nuit on trouvera', "Quelque part où déposer les fleurs qu'on a cueillies", "Pars avant l'aube", 'Quand la lumière veut nous voir', "Quelque part dans le monde un oiseau s'endort", 'Sans bruit, toi et moi', 'Dans la nuit on trouvera', "Quelque part où déposer les fleurs qu'on a cueillies", "Pars avant l'aube", 'Quand la lumière veut nous voir', "Quelque part dans le monde un oiseau s'endort", 'Sans bruit, toi et moi', 'Dans la nuit on trouvera', 'Quelque part où déposer les fleurs']

list2 = ["I'm driving your car with you sleeping in the seat next to me", 'Like a baby, you twist and you turn', "You're travelling fast like a bird in a dream", 'Look at it go, look at it dance over the sky like a rocket', 'A love machine, a cinematic dream', 'So pure and it hurts when the beauty is lost in the speed', "'Cause everything matters to me", '(To me, to me, to me, to me)', 'To me', "You're a part of the dawn where the light comes from the dark", "You're a part of the morning and everything matters", 'And we are an atom and a star', "You're a part of the movement and everything matters", "I'm watching your storm turn into form", 'And the clouds of the world like a burst', 'It dances and it twirls', 'On top of the world, it is good and it hurts', 'A teacher, a simulated dream', 'A cure, a cure for the hurt', 'And the pleasure you feel is real', '(To me, to me, to me, to me, to me)', "Quelque part, avant l'aube", 'Quand la lumière veut nous voir', "Quelque part dans le monde un oiseau s'endort", 'Sans bruit, toi et moi', 'Dans la nuit on trouvera', "Quelque part où déposer les fleurs qu'on a cueillies", "Pars avant l'aube", 'Quelque part où déposer les fleurs']

list3 = ["I'm driving your car with you sleeping in the seat next to me", 'Like a baby, you twist and you turn', "You're travelling fast like a bird in a dream", 'Look at it go, look at it dance over the sky like a rocket', 'A love machine, a cinematic dream', 'So pure and it hurts when the beauty is lost in the speed', "'Cause everything matters to me", '(To me, to me, to me, to me)', 'To me', "You're a part of the dawn where the light comes from the dark", "You're a part of the morning and everything matters", 'And we are an atom and a star', "You're a part of the movement and everything matters", " ", "I'm watching your storm turn into form", 'And the clouds of the world like a burst', 'It dances and it twirls', 'On top of the world, it is good and it hurts', ' ', 'A teacher, a simulated dream', 'A cure, a cure for the hurt', 'And the pleasure you feel is real', ' ', ' ', ' ', ' ', ' ', '(To me, to me, to me, to me, to me)', "Quelque part, avant l'aube", 'Quand la lumière veut nous voir', "Quelque part dans le monde un oiseau s'endort", 'Sans bruit, toi et moi', 'Dans la nuit on trouvera', "Quelque part où déposer les fleurs qu'on a cueillies", "Pars avant l'aube", ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'Quelque part où déposer les fleurs']

List1 is the complete list. It has all the lines. List2, however is the incomplete list. It has some of the lines in list1, however some lines are missing. List3 is what the result should look like. Now how do I create a function that searches through List1 and List2, when it finds a line not in list2, inserts the placeholder ' ' so that at the end of everything, the output is exactly List3. Please help.

CodePudding user response:

You need to go over list1 & list2 in parallel, and all time there is a match - add to list3. If there is no match - append the placeholder and go forward in list1. I guess it would be something like this:

list_result = []
i = 0
j = 0
while i < len(list1) and j < len(list2):
    if list1[i] == list2[j]:
        list_result.append(list1[i])
        i  = 1
        j  = 1
    else:
        list_result.append(' ')
        i  = 1
diff = len(list1) - len(list_result)
for k in range(diff):
    list_result.append(' ')

CodePudding user response:

you can play around with following in order to get more specific answer

def missing_words(list1, list2):
    for i in range(len(list1)):
        if list1[i] != list2[i]:
           list2.insert(i,  ' ')

missing_words(list1, list2)
if list2==list3:
    print("yess")
  • Related