从嵌套列表中重新列出项目

2024-04-23 08:03:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在想怎么摘这些水果,比如:

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

如何输出:

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

Tags: idappletypefruitorangeapples水果corn
2条回答

operator.itemgetter(1)可以是higher-performance

“琐碎的答案转换成评论”?!世界跆拳道联盟!)这是一条线的现实。为了性能,更喜欢itemgetter而不是lambda表达式。如果可以,请向我们展示此代码出现的更多上下文。你知道吗

简单到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']
>>>

相关问题 更多 >