开发者

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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜