Spliting a list into sizes specified by another list
问题 Let say there's a list X and another list num_items that specify the number of items that should be in the sublist, I can split the list manually as such: >>> x = list(range(10)) >>> x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> num_items = [4, 4, 2] >>> slice1 = x[:num_items[0]] >>> slice2 = x[len(slice1):len(slice1)+num_items[1]] >>> slice3 = x[len(slice1)+len(slice2):] >>> slice1, slice2, slice3 ([0, 1, 2, 3], [4, 5, 6, 7], [8, 9]) There will be two cases where the last few slices can become