Skip to main content
 首页 » 编程设计

python之重新列出嵌套列表中的项目

2025年05月04日16zdz8207

我想知道如何采摘所有水果,例如:

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

如何输出:

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

请您参考如下方法:

像 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'] 
>>>