简体   繁体   中英

alternate 2 list when one list is longer python

right now the code is able to make a sentence alternating through the list if both list are the same length. But it wont run if the list are different lengths. I want the longer list to continue printing one they are done alternating.

def intersperse():
    one = str(input("enter a sentence"))
    two = str(input("enter a sentence"))

    a = one.split()
    b = two.split()
    sentence = " "

    #min_len = min(len(a),len(b))
    if len(a) > len(b):

        min_len = a
    else:
        min_len = b

    for i in min_len:
        sentence += a.pop(0) + " " + b.pop(0) + " "

    print(sentence)



intersperse()

You could do something like this:

def intersperse(one, two):
    a = one.split()
    b = two.split()
    sentence = " "

    if len(a) < len(b):
        while a:
            sentence += a.pop(0) + " " + b.pop(0) + " "
        while b:
            sentence += b.pop(0) + " "
    else:
        while b:
            sentence += a.pop(0) + " " + b.pop(0) + " "
        while a:
            sentence += a.pop(0) + " "

    print(sentence)


one = 'This is the long sentence'
two = 'Short sentence'

intersperse(one, two)

Output

This Short is sentence the long sentence 

Note that the code above is just an example of what you can do inside intersperse . A more pythonic alternative, involves using zip_longest :

from itertools import zip_longest


def intersperse(one, two):
    a = one.split()
    b = two.split()
    sentence = ' '.join([e for pair in zip_longest(a, b) for e in pair if e])
    print(sentence)


one = 'This is the long sentence'
two = 'Short sentence'

intersperse(one, two)

Output

This Short is sentence the long sentence

You just need to handle the case when you run out of words. Also things like sentence1 = ''

sentence1 = "a bee went buzz"
sentence2 = "a dog went on the bus to Wagga Wagga"

# Single space all whitespace, then split on space
words1 = ' '.join(sentence1.split()).split()
words2 = ' '.join(sentence2.split()).split()

# What is the maximum number of outputs
max_len = max( len(words1), len(words2) )

# Loop through all our words, using pairs, but handling 
# differing sizes by skipping
sentence = ''
for i in range(max_len):
    if (len(words1) > 0):
        w1 = words1.pop(0)
        sentence += w1 + ' '
    if (len(words2) > 0):
        w2 = words2.pop(0)
        sentence += w2 + ' '

print(sentence)
from itertools import zip_longest
for one_el, two_el in zip_longest(a, b):
   one_el = one_el or " "
   two_el = two_el or " "
   sentence += one_el + " " + two_el + " "

Here is a way how to do it with slice.

def intersperse(one, two):
    a = one.split()
    b = two.split()

    sentence = [None for i in range(len(a) + len(b))]

    min_len = min(len(a), len(b))
    sentence[:2*min_len:2] = a[:min_len]
    sentence[1:2*min_len:2] = b[:min_len]

    rest = a[min_len:] if len(a) > min_len else b[min_len:]
    sentence[2*min_len:] = rest

    return " ".join(sentence)

print(intersperse("a aa aaa", "b"))
print(intersperse("a aa aaa", "b bb"))
print(intersperse("a aa aaa", "b bb bbb"))
print(intersperse("a aa aaa", "b bb bbb bbbb"))

Output:

a b aa aaa
a b aa bb aaa
a b aa bb aaa bbb
a b aa bb aaa bbb bbbb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM