开发者

List Compherension with sublist [closed]

开发者 https://www.devze.com 2023-03-15 04:47 出处:网络
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 6 years ago.

开发者_如何学编程 Improve this question

Suppose I have a list of sublist:

lst = [ ['A', 'is', 'from', 'B,', '2', 'm', 'from', 'C', '1.2', 'm', 'from', 'D.'], 
        ['0.3', 'm', 'from', 'D.'] ]

and I wanted to organize the letters after the word "from" so I want to have

new_lst = [ [B,C,D], [D] ]


As you're talking about list comprehensions, you're probably writing in Python.

So I wrote the most awesome comprehension you may find to solve that problem.

>>> [[next(i) for j in i if j == 'from'] for i in (iter(x) for x in lst)]
[['B,', 'C', 'D.'], ['D.']]

List Compherension with sublist [closed]

0

精彩评论

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