如何创建一个搜索两个列表的函数,在列表中缺少项目的地方添加一个占位符,如 ' ' 或 'missing item'?
How can I create a function that searches through two lists, adding a placeholder like ' ' or 'missing item' in the list where the item is missing?
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 是完整列表。它有所有的线条。
然而,List2 是不完整的列表。它有 list1 中的一些行,但是缺少一些行。
List3 是结果的样子。现在我如何创建一个搜索 List1 和 List2 的函数,当它找到不在 list2 中的行时,插入占位符 ' ',以便在所有内容的末尾,输出正好是 List3。请帮忙
您需要并行检查 list1 和 list2,并且始终有匹配项 - 添加到 list3。如果没有匹配 - 附加占位符并在 list1 中前进。我想应该是这样的:
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(' ')
您可以尝试关注以获得更具体的答案
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")
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 是完整列表。它有所有的线条。 然而,List2 是不完整的列表。它有 list1 中的一些行,但是缺少一些行。 List3 是结果的样子。现在我如何创建一个搜索 List1 和 List2 的函数,当它找到不在 list2 中的行时,插入占位符 ' ',以便在所有内容的末尾,输出正好是 List3。请帮忙
您需要并行检查 list1 和 list2,并且始终有匹配项 - 添加到 list3。如果没有匹配 - 附加占位符并在 list1 中前进。我想应该是这样的:
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(' ')
您可以尝试关注以获得更具体的答案
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")