Python reshape a list to multidimensional list

后端 未结 3 1079
有刺的猬
有刺的猬 2021-01-06 00:16

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 wit

3条回答
  •  醉梦人生
    2021-01-06 00:21

    If you want to process them in a loop, you can just do this:

    list1=[[2,3,4],[1],[77,8,27,12],[25,15]]
    
    list2=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
    
    last = 0
    
    for ele in list1:
        print(ele, list2[last : last + len(ele)])
        last += len(ele)
    

    Result:

    ([2, 3, 4], ['a', 'b', 'c'])
    ([1], ['d'])
    ([77, 8, 27, 12], ['e', 'f', 'g', 'h'])
    ([25, 15], ['i', 'j'])
    

提交回复
热议问题