简体   繁体   中英

What is the most pythonic way to iterate through a long list of strings and structure new lists from that original list?

I have a large list of strings of song lyrics. Each element in the list is a song, and each song has multiple lines and some of those lines are headers such as '[Intro]', '[Chorus]' etc. I'm trying to iterate through the list and create new lists where each new list is comprised of all the lines in a certain section like '[Intro]' or '[Chorus]'. Once I achieve this I want to create a Pandas data frame where each row are all the song lyrics and each column is that section(Intro, Chorus, Verse 1, etc.) of the song. Am I thinking about this the right way? Here's an example of 1 element in the list and my current partial attempt to iterate and store:

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)

Refer to python's doc: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

you could also use this

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

See top answer here: Understanding slice notation

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