List Compherension with sublist [closed]
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 questionSuppose 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.']]
精彩评论