【发布时间】: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