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", "1.1.1.2", "1.1.1.3", "1.1.1.4"]
list1 contains the elements which are the sizes of chunks which I wish to divide the list2 in.
解决方案
Yet another solution
list1 = [1,2,1]
list2 = ["1.1.1.1","1.1.1.2","1.1.1.3","1.1.1.4"]
chunks = []
count = 0
for size in list1:
chunks.append([list2[i+count] for i in range(size)])
count += size
print(chunks)
# [['1.1.1.1'], ['1.1.1.2', '1.1.1.3'], ['1.1.1.4']]