Splitting a list into uneven groups?

前端 未结 6 1986
北海茫月
北海茫月 2020-12-14 20:47

I know how to split a list into even groups, but I\'m having trouble splitting it into uneven groups.

Essentially here is what I have: some list, let\'s call it

6条回答
  •  悲哀的现实
    2020-12-14 21:05

    This solution keeps track of how many items you've written. It will crash if the sum of the numbers in the second_list is longer than mylist

    total = 0
    listChunks = []
    for j in range(len(second_list)):
        chunk_mylist = mylist[total:total+second_list[j]]
        listChunks.append(chunk_mylist)
        total += second_list[j]
    

    After running this, listChunks is a list containing sublists with the lengths found in second_list.

提交回复
热议问题