Oct-25-2018, 09:40 PM
Hi all,
I am trying to split the given list into multuple lists where the value limit for such splitted-list will be 10.
For example, given a list, eg.
It will either fills up with as many as integers that adds up to 10, or integers closest to 10..
And if there are any integers not used, those integers will be appended in another list and so the results would be something as follows:
I am trying to split the given list into multuple lists where the value limit for such splitted-list will be 10.
For example, given a list, eg.
init_list = [1, 2, 3, 1, 7, 4, 5, 11]
, I am trying to split init_list
into 3 different lists where each list each list only holds up to a max value of 10. It will either fills up with as many as integers that adds up to 10, or integers closest to 10..
And if there are any integers not used, those integers will be appended in another list and so the results would be something as follows:
list01 = [1, 1, 2, 3, 4] list02 = [7] list03 = [5] # unused unused = [11]So far I have managed to get it to split the list into 3 different lists, but I am unable to get the portion of setting the max value of 10 or, appending only the integer(s) closest to 10..
init_list = [1, 2, 3, 1, 7, 4, 5, 11] def split_list(given_list, n): result = [[] for i in range(n)] sums = [0]*n i = 0 for e in given_list: result[i].append(e) sums[i] += e i = sums.index(min(sums)) return result split_list(init_list, 3)Not exactly sure if this is the right way to approach this, or if anyone can kindly advise any other topics that I can try looking into?