I have a list of integers and I'm trying to group them into sums of thirty, and then yield an output of all the smaller groups (which now equal thirty).
I'm a python novice and the only way I can think of doing this so far is sorting the Master List, getting groups of like numbers using itertools.groupby, and then creating a bunch of conditionals to find 30. Like so:
from itertools import groupby
N =[1,2,3,4,3,2,3,4,6,5,3,2,4,6,4,12,2,4,5,6,7,45,23,3,4,6,22,5,4,3,21,3,4,11]
N.sort()
print ([list(j) for i, j in groupby(N)])
Obviously, I already foresee some problems, not the least of which is that this code is extremely inefficient.
I was hoping for some suggestions to create a code that would group integers to sum to thirty without replacement - so once a integer is assigned to a group it cannot be reused.
So, given a list of L = [12,18,2,22,14,6,4,7,5]
I would get the output: [12,18] [2,22,6] [14,4,7,5]
Also just a side question:
The Master List is actually the values of a dictionary. Once these values are regrouped, do they preserve their respective keys? I hope to eventually have the output be the keys of the values grouping to thirty.
So referring back to the top example:
So, given a dictionary of L = {"A":12,"B":18,"C":2,"D":22,"E":14,"F":6,"G"4,"H":7,"I":5}
I would get the output: [A,B] [C,D,F] [E,G,H,I]
Thank you in advance!!