【问题标题】:join a list of of sentences into phrases将句子列表加入短语
【发布时间】:2018-10-11 23:43:23
【问题描述】:

我有一个歌词字符串列表,如下所示:

[' 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']

即每个歌词都以换行符结尾,有些以句点和换行符结尾。

我想做的是生成一个如下所示的列表:

[' 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 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 

此代码适用于需要以上述格式连接的歌词的各个部分。

我很难将此功能推广到整个原始歌词列表。任何建议将不胜感激。

**注意传递给函数的歌词是正确排序的,即按照歌曲排序。

【问题讨论】:

  • @我不确定该链接在这里如何应用?
  • @Prune 你能找到更好的链接吗?

标签: python list for-loop while-loop append


【解决方案1】:

如果我理解正确的话,只需

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

【讨论】:

  • 这不是 OP 想要的,因为句子中会缺少句号。
  • 这肯定在输出中有一些偏差。查看最后一个元素。
【解决方案2】:

您可以将itertools.groupby 与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)]

输出:

[' 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']

【讨论】:

    【解决方案3】:

    这是使用for 循环的一种方式。

    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']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多