2013-10-23 54 views
1

我想一个项目,以图如何挑选所有的水果,如:重新列出从嵌套列表

[['id', 'sub type', 'type'], ['1', 'apples', 'fruit'], ['15', 'orange', 'fruit'], ['3', 'corn', 'vegtable']] 

我如何输出:

['sub type','apple','orange','corn'] 
+1

玉米不是水果:) – roippi

+1

@roippi是的!这会引发一个错误:'TypeError:Corn不是水果'。 – aIKid

+1

任何人都尝试'西红柿'...? – smci

回答

4

简单如list comprehension

>>> lst = [['id', 'sub type', 'type'], ['1', 'apples', 'fruit'], ['15', 'orange', 'fruit'], ['3', 'corn', 'vegtable']] 
>>> [x[1] for x in lst] 
['sub type', 'apples', 'orange', 'corn'] 
>>> 
+0

哇那么简单 – George

2

operator.itemgetter(1)可以higher-performance

'微不足道的答案转换为评论'?! WTF!)这是单线现实。为了提高性能,最好将itemgetter设置为lambda表达式。如果可以,请向我们展示此代码发生的更多上下文。