Tonya Howe
Tonya Howe

Reputation: 55

how to retrieve and store a list of lists extracted from a list by partial string match?

I'm trying to pull out all the lines from a text file (which is a list of lines) that occur between list items containing the string 'Chapter '.

My data, content_lines, looks like this:

['Chapter 1', 'Sir Walter Elliot.....', 'Another paragraph of text.', 'And another....', 'Chapter 2', 'Paragraph starting chapter 2', 'Another paragraph in chapter 2']

I want to iterate over all the chapters and create a list of lists, where each list contains only the items between 'Chapter ' and 'Chapter '. So, something that looks like this:

[['Sir Walter Elliot.....', 'Another paragraph of text.', 'And another....'], ['Paragraph starting chapter 2', 'Another paragraph in chapter 2']]

So far, I have this:

l = content_lines
wanted = 'Chapter '


result = list(filter(lambda x: x.startswith(wanted) is False, l))

That gets me a list of all the lines that don't contain 'Chapter ', which is a good start, but it's not what I'm looking for. I'm new to this, and I know I'll have to create a loop of some kind, but my searches just aren't working for me. Can you help?

Upvotes: 1

Views: 44

Answers (2)

DJRahim
DJRahim

Reputation: 125

This is what I came up with, hope it helps.

list = ['Chapter 1', 'Sir Walter Elliot.....', 'Another paragraph of text.', 'And another....', 'Chapter 2', 'Paragraph starting chapter 2', 'Another paragraph in chapter 2']

i = 0
res = []

for x in list:
    if "Chapter" in x:
        i+=1
        res.append([])
    else:
        res[i-1].append(x)
        
print(res)

Upvotes: 0

not_speshal
not_speshal

Reputation: 23156

Try iterating and creating your output:

output = list()
for line in content_lines:
    if line.startswith("Chapter"):
        output.append([])
    else:
        output[-1].append(line)

>>> output
[['Sir Walter Elliot.....', 'Another paragraph of text.', 'And another....'],
 ['Paragraph starting chapter 2', 'Another paragraph in chapter 2']]

Upvotes: 4

Related Questions