Python将列表重塑为多维列表 [英] Python reshape a list to multidimensional list
本文介绍了Python将列表重塑为多维列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表,每个维度的长度都不同,如下所示:
I have a list that have different length in each dimension like below:
list1=[[2,3,4],[1],[77,8,27,12],[25,15]]
还有另一个具有相同元素数量的列表,如:
and I have another list with the same number of element like:
list2=[a,b,c,d,e,f,g,h,i,j]
我想将list2重塑为list1,并在for循环中一起处理两个列表.
I want to reshape my list2 as list1 and to process two lists together in a for loop.
推荐答案
这是一种可爱的方式.
list1 = [[2,3,4],[1],[77,8,27,12],[25,15]]
list2 = list("abcdefghij")
list2_iterator = iter(list2)
list2_reshaped = [[next(list2_iterator) for _ in sublist] for sublist in list1]
print(list2_reshaped)
Out: [['a', 'b', 'c'], ['d'], ['e', 'f', 'g', 'h'], ['i', 'j']]
我不知道单纯的理解是否有可能.
I don't know if it's possible with pure comprehensions.
这篇关于Python将列表重塑为多维列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文