-2

I was given homework to write a Python program that allows the user to enter two sentences, and then puts the words of both sentences into a list in an interleaved fashion, and prints it, as in this example:

Sentence 1: Today I went to the beach

Sentence 2: Tomorrow I will travel to Europe

["Today", "Tomorrow", "I", "I", "went", "will", "to","travel", "the", "to", "beach", "Europe"]

I tried this, but dosen't work well with two phrases with diferent lenght

from itertools import cycle

list1=[]

list2=[]

phrase1=str(input("Enter phrase 1: "))

phrase2=str(input("Enter phrase 2: "))

list1=phrase1.split()

list2=phrase2.split()

print("The phrase 1 is: " + str(phrase1))

print("The phrase 2 is: " + str(phrase2))

res = [ele for comb in zip(lista1, lista2) for ele in comb]

print("Interleaved List : " + str(res))
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • What's your problem,this code seems working fine – flyingfox Oct 05 '22 at 04:57
  • 1
    [ask] and [mre] – Julien Oct 05 '22 at 05:06
  • Does this answer your question? [How to interleave two lists of different length?](https://stackoverflow.com/questions/48199961/how-to-interleave-two-lists-of-different-length) – ILS Oct 05 '22 at 05:28
  • 1
    What should the output look like if the lists contain different numbers of elements? Please clarify with an example. Also, no need to call *str()* on the return value from *input()* as it's already a string type – DarkKnight Oct 05 '22 at 06:11

2 Answers2

0

For phrases with a different length, you can use itertools.zip_longest

from itertools import cycle, zip_longest

list1=[]

list2=[]

phrase1=str(input("Enter phrase 1: "))

phrase2=str(input("Enter phrase 2: "))

list1=phrase1.split()

list2=phrase2.split()

print("The phrase 1 is: " + str(phrase1))

print("The phrase 2 is: " + str(phrase2))

res = [ele for comb in zip_longest(list1, list2) for ele in comb]

print("Interleaved List : " + str(res))

This will output as follows:

Enter phrase 1: Today I went to the beach

Enter phrase 2: Tomorrow I will travel to Europe and America

The phrase 1 is: Today I went to the beach

The phrase 2 is: Tomorrow I will travel to Europe and America

Interleaved List : ['Today', 'Tomorrow', 'I', 'I', 'went', 'will', 'to', 'travel', 'the', 'to', 'beach', 'Europe', None, 'and', None, 'America']

tasrif
  • 78
  • 8
  • Thanks, i add it this for remove the "None" print("Interleaved List : " + str(list(filter(lambda x: x is not None, res)))) – Manuel Deza Oct 06 '22 at 18:00
0

The way you do this will depend on what the result should look like when the two input string do not have an equal number of elements. You may want something like this:

from itertools import zip_longest

s1 = 'Today I went to the sandy beach'
s2 = 'Tomorrow I will travel to Europe'

result = []

for w1, w2 in zip_longest(s1.split(), s2.split()):
    if w1:
        result.append(w1)
    if w2:
        result.append(w2)

print(result)

Output:

['Today', 'Tomorrow', 'I', 'I', 'went', 'will', 'to', 'travel', 'the', 'to', 'sandy', 'Europe', 'beach']
DarkKnight
  • 19,739
  • 3
  • 6
  • 22