将列表列表的列表展平到列表列表 [英] flatten list of lists of lists to a list of lists
本文介绍了将列表列表的列表展平到列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I've already searched SO for how to flatten a list of lists (i.e. here:Making a flat list out of list of lists in Python) but none of the solutions I find addresses flattening a list of lists of lists to just a list of lists.
我有:
my_list = [ [ [1,2,3],[4,5] ], [ [9],[8,9,10],[3,4,6] ], [ [1] ] ]
我想要:
my_list = [ [1,2,3,4,5], [9,8,9,10,3,4,6], [1] ]
该解决方案也应适用于浮点数列表.有什么建议吗?
The solution should work for a list of floats as well. Any suggestions?
推荐答案
如果我们应用此答案中的逻辑,不应该只是:
If we apply the logic from this answer, should not it be just:
In [2]: [[item for subsublist in sublist for item in subsublist] for sublist in my_list]
Out[2]: [[1, 2, 3, 4, 5], [9, 8, 9, 10, 3, 4, 6], [1]]
而且,类似地,通过 itertools.chain()
:
And, similarly via itertools.chain()
:
In [3]: [list(itertools.chain(*sublist)) for sublist in my_list]
Out[3]: [[1, 2, 3, 4, 5], [9, 8, 9, 10, 3, 4, 6], [1]]
这篇关于将列表列表的列表展平到列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文