How to divide python list into sublists of unequal length?

后端 未结 5 1368
情书的邮戳
情书的邮戳 2021-01-25 22:30

I am trying to divide a list of elements which are comma separated into chunks of unequal length. How can I divide it?

list1 = [1, 2, 1]
list2 = [\"1.1.1.1\", \"         


        
5条回答
  •  一整个雨季
    2021-01-25 23:24

    Something like:

    def divideUnequal(list1, list2):
        counter=0
        step=0
        divided=[]
        for count in list1:
                step= counter+ count
                sublist= list2[counter: step]
                counter= step
                divided.append(sublist)
        return divided
    

提交回复
热议问题