从 'list of lists of lists' 到 'list of lists' - Python
From 'list of lists of lists' to 'list of lists' - Python
我有一个问题无法解决。我需要列表列表而不是列表列表列表? 'unlists' 一级 'deep' 列表在 Python 中是否有任何简单的命令?这是我拥有的:
[[['this', 'is', 'the', 'good', 'machine', 'learning', 'book']],
[['this', 'is', 'another', 'book']]]
以及我需要去的地方:
[['this', 'is', 'the', 'good', 'machine', 'learning', 'book'],
['this', 'is', 'another', 'book']]
我试图用下面的函数解决它
from pandas.core.common import flatten
list(flatten(x))
...但它将所有内容分解为一个列表...
['this', 'is', 'the', 'good', 'machine', 'learning', 'book', 'this', 'is', 'another', 'book']
请帮忙:)
[item for sub_list in your_list for item in sub_list]
您可以简单地将其仅应用于子列表:
[list(flatten(y)) for y in x]
我有一个问题无法解决。我需要列表列表而不是列表列表列表? 'unlists' 一级 'deep' 列表在 Python 中是否有任何简单的命令?这是我拥有的:
[[['this', 'is', 'the', 'good', 'machine', 'learning', 'book']],
[['this', 'is', 'another', 'book']]]
以及我需要去的地方:
[['this', 'is', 'the', 'good', 'machine', 'learning', 'book'],
['this', 'is', 'another', 'book']]
我试图用下面的函数解决它
from pandas.core.common import flatten
list(flatten(x))
...但它将所有内容分解为一个列表...
['this', 'is', 'the', 'good', 'machine', 'learning', 'book', 'this', 'is', 'another', 'book']
请帮忙:)
[item for sub_list in your_list for item in sub_list]
您可以简单地将其仅应用于子列表:
[list(flatten(y)) for y in x]