gEr
gEr

Reputation: 215

Python list comprehension troubles

I have some text manipulation to do, here's a for loop that does it:

 for p in paras[:]:

     pidx = paras.index(p)

     for sent in p:
         sidx = p.index(sent)

         sent = ' '.join(w[0] for w in sent)
         paras[pidx][sidx] = sent

     paras[pidx] =  'start' + ' '.join(paras[pidx]) + 'end'

Here is my list comprehension:

 [' '.join(w[0] for w in sent) for p in paras for sent in p]

This returns one large list of sentences and I need separate lists based on the paragraph (p) or have some way to signify where the end of each para is. Is there some sexy way to do this?

Upvotes: 0

Views: 179

Answers (2)

Michael Dillon
Michael Dillon

Reputation: 32392

If a sentence is a list of words, then:

["see", "spot", "run"]

is a sentence. And if a paragraph is a list of sentences, then:

[ ["see", "spot", "run"], ["see", "dick", "run"], ["see" "dick", "run", "after", "spot"] ]

is a paragraph. You just need to construct a list of lists of words, instead of a list of words.

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304137

A nested LC

[[' '.join(w[0] for w in sent) for sent in p] for p in paras]

should give each para as a separate list

Upvotes: 2

Related Questions