Andrew Louis
Andrew Louis

Reputation: 303

join a list of of sentences into phrases

I have a list of song lyric strings that looks like this:

[' Extending a life\n', 'With total resistance\n', 
'To fatal disease\n', 'Future methods of science.\n', 
'Replacing what is real\n', 'By using technology\n', 
'Population control\n', 'Selecting those who will breed.\n',
'A specific type of form\n', 'Chosen for the unborn.\n', 
'A mind without emotion\n', 'Progressive anatomy.\n']

I.e. each lyric ends with a newline character, and some end in a period and a new line character.

What I would like to do is produce a list that looks like this:

[' Extending a life\n With total resistance\n To fatal disease\n Future methods of science.\n', 
'Replacing what is real\n By using technology\n Population control\n Selecting those who will breed.\n',
'A specific type of form\n Chosen for the unborn.\n', 
'A mind without emotion\n Progressive anatomy.\n'] 

I.e. Each value of the new list is a complete lyric string with a period at the end of each complete lyric.

I have an idea on how to do go about this for only one section of lyrics:

def random_lyrics(song):
    i = 1
    formatted_song = []
    formatted_song.append(song[0][1:])
    while i <= len(song)-1:
        if song[i][len(song[i])-2] == '.':
            formatted_song[0] += song[i]
            break
        else:
            formatted_song[0] += song[i]
            i +=1 

This code works for individual sections of lyrics that need to be joined in the format described above.

I am having a hard time generalizing this function to work on the entire original list of song lyrics. Any advice would be appreciated.

**Note that the lyrics passed into the function are ordered correctly, i.e. ordered according to the song.

Upvotes: 1

Views: 58

Answers (3)

bphi
bphi

Reputation: 3195

If I understand you correctly, it can be accomplished with just

[s + '.\n' for s in ' '.join(lyrics).split('.\n')[:-1]]

Upvotes: 1

jpp
jpp

Reputation: 164673

This is one way using a for loop.

lst = [' Extending a life\n', 'With total resistance\n', 
       'To fatal disease\n', 'Future methods of science.\n', 
       'Replacing what is real\n', 'By using technology\n', 
       'Population control\n', 'Selecting those who will breed.\n',
       'A specific type of form\n', 'Chosen for the unborn.\n', 
       'A mind without emotion\n', 'Progressive anatomy.\n']

def formatter(x):
    res = []
    part = []
    for i in x:
        part.append(i)
        if i[-2] == '.':
            res.append(part[:])
            part.clear()
    return [''.join(j) for j in res]

res = formatter(lst)

[' Extending a life\nWith total resistance\nTo fatal disease\nFuture methods of science.\n',
 'Replacing what is real\nBy using technology\nPopulation control\nSelecting those who will breed.\n',
 'A specific type of form\nChosen for the unborn.\n',
 'A mind without emotion\nProgressive anatomy.\n']

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

You can use itertools.groupby with re:

import re
import itertools
d = [' Extending a life\n', 'With total resistance\n', 'To fatal disease\n', 'Future methods of science.\n', 'Replacing what is real\n', 'By using technology\n', 'Population control\n', 'Selecting those who will breed.\n', 'A specific type of form\n', 'Chosen for the unborn.\n', 'A mind without emotion\n', 'Progressive anatomy.\n']
results = [list(b) for _, b in itertools.groupby(d, key=lambda x:bool(re.findall('\.\n', x)))]
final_result = [' '.join(results[i]+results[i+1]) for i in range(0, len(results), 2)]

Output:

[' Extending a life\n With total resistance\n To fatal disease\n Future methods of science.\n', 'Replacing what is real\n By using technology\n Population control\n Selecting those who will breed.\n', 'A specific type of form\n Chosen for the unborn.\n', 'A mind without emotion\n Progressive anatomy.\n']

Upvotes: 0

Related Questions