我想一个项目,以图如何挑选所有的水果,如:重新列出从嵌套列表
[['id', 'sub type', 'type'], ['1', 'apples', 'fruit'], ['15', 'orange', 'fruit'], ['3', 'corn', 'vegtable']]
我如何输出:
['sub type','apple','orange','corn']
我想一个项目,以图如何挑选所有的水果,如:重新列出从嵌套列表
[['id', 'sub type', 'type'], ['1', 'apples', 'fruit'], ['15', 'orange', 'fruit'], ['3', 'corn', 'vegtable']]
我如何输出:
['sub type','apple','orange','corn']
>>> 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']
>>>
哇那么简单 – George
operator.itemgetter(1)
可以higher-performance
('微不足道的答案转换为评论'?! WTF!)这是单线现实。为了提高性能,最好将itemgetter设置为lambda表达式。如果可以,请向我们展示此代码发生的更多上下文。
玉米不是水果:) – roippi
@roippi是的!这会引发一个错误:'TypeError:Corn不是水果'。 – aIKid
任何人都尝试'西红柿'...? – smci