迭代一长串字符串并从原始列表中构造新列表的最Python方法是什么



我有一大串歌词。列表中的每个元素都是一首歌,每首歌都有多行,其中一些行是标题,如"[Intro]"、"[Chorus]"等。一旦我做到了这一点,我想创建一个Pandas数据帧,其中每一行都是歌曲的所有歌词,每一列都是歌曲中的那个部分(简介、合唱、第1节等(。我这样想对了吗?下面是列表中1个元素的示例,以及我当前迭代和存储的部分尝试:

song_index_number = 0
line_index_in_song = 0
intro = []
bridge = []
verse1 = []
prechorus = []
chorus = []
verse2 = []
verse3 = []
verse4 = []
verse5 = []
outro = []
lyrics_by_song[30]
['[Intro]',
'(Just the two of us, just the two of us)',
'Baby, your dada loves you',
"And I'ma always be here for you",
'(Just the two of us, just the two of us)',
'No matter what happens',
"You're all I got in this world",
'(Just the two of us, just the two of us)',
"I would never give you up for nothin'",
'(Just the two of us, just the two of us)',
'Nobody in this world is ever gonna keep you from me',
'I love you',
'',
'[Verse 1]',
"C'mon Hai-Hai, we goin' to the beach",
'Grab a couple of toys and let Dada strap you in the car seat',
"Oh, where's Mama? She's takin' a little nap in the trunk",
"Oh, that smell? Dada must've runned over a skunk",
"Now, I know what you're thinkin', it's kind of late to go swimmin'",
"But you know your Mama, she's one of those type of women",
"That do crazy things, and if she don't get her way, she'll throw a fit",
"Don't play with Dada's toy knife, honey, let go of it (No)",
"And don't look so upset, why you actin' bashful?",
"Don't you wanna help Dada build a sandcastle? (Yeah)",
'And Mama said she wants to show you how far she can float',
"And don't worry about that little boo-boo on her throat",
"It's just a little scratch, it don't hurt",
"Her was eatin' dinner while you were sweepin'",
'And spilled ketchup on her shirt',
"Mama's messy, ain't she? We'll let her wash off in the water",
"And me and you can play by ourselves, can't we?",
'',
'[Chorus]',
'Just the two of us, just the two of us',....
for line in lyrics_by_song:
if lyrics_by_song == '[Intro]':
intro.append(line)

请参阅python的文档:https://docs.python.org/3/tutorial/datastructures.html#list-综合

你也可以使用这个

Intro = lyrics_by_song[lyrics_by_song.index('[Intro]'):lyrics_by_song.index('something_else')]

请参阅此处的首要答案:了解切片表示

最新更新