ryanvolap
ryanvolap

Reputation: 77

Split a List into several Lists based on sum of List in python

This is my code:

list_ = [30.3125, 13.75, 12.1875, 30.625, 18.125, 58.75, 38.125, 33.125, 55.3125, 28.75, 60.3125, 31.5625, 59.0625]
total = 150.0
new_list = []

while sum(list_) > total:
    new_list.append(list_[-1:])
    list_ = list_[:-1]

new_list.reverse()

print(list_)
>>> [30.3125, 13.75, 12.1875, 30.625, 18.125]
print(new_list)
>>> [58.75, 38.125, 33.125, 55.3125, 28.75, 60.3125, 31.5625, 59.0625]

My question is I want to repeat the code for the new_list just created, but I don't know how.( I want it automatically split the list when the sum of the values in a list greater than total.)

I want the result like this.

>>> list_     = [30.3125, 13.75, 12.1875, 30.625, 18.125]
>>> new_list  = [58.75, 38.125, 33.125]
>>> new_list1 = [55.3125, 28.75, 60.3125]
>>> new_list2 = [31.5625, 59.0625]

Thanks all.

Upvotes: 0

Views: 993

Answers (2)

danMontelo
danMontelo

Reputation: 1

I am using the code from the solution above, and creating 'splits' and 'sums' of the sublists. I'm getting sums of sublists that are exceeding the 'max'. But I'm new to programming so it's a challenge to troubleshoot.

Specifically, the 4th list is totaling as 42079.94, which is greater than 'max' of 40000.

My list is quite long:

rounded = ['2173.46', '5160.07', '4967.87', '4956.85', '5368.52', '5416.99', '5769.32', '5416.99', '6168.42', '5416.99', '2551.58', '3968.48', '5416.99', '6970.02', '5416.99', '7370.98', '5416.99', '7774.94', '5368.75', '8093.52', '5802.37', '5651.04', '5789.39', '5638.3', '5789.39', '5315.93', '5789.39', '5253.33', '5782.12', '5253.33', '5810.41', '5253.33', '5900.1', '5253.33', '5900.1', '5253.33', '5815.67', '5253.33', '5788.27', '5253.33', '5912.87', '5366.49', '6830.97', '6841.2', '6385.11', '6163.01', '6419.96', '6836.72', '5886.96', '1809.82', '2060.69', '2495.1', '2495.62', '6767.83', '6836.72', '2309.49', '1075.67', '1075.67', '2061.71', '1075.67', '1075.67']
dict_ = {}

sum_ = 0
i = 0
max = 40000

for item in rounded:    
    # When sum + item > max reset sum and go to next key
    sum_ += item
    if sum_ + item > max:
        sum_ = 0
        i+= 1
    dict_.setdefault(i, []).append(item)

splits = []
for i in range(0,len(dict_)):
    splits.append(len(dict_[i]))

sums = []
for i in range(0,len(dict_)):
    sums.append(sum(dict_[i]))

print sums
sums = ['33813.08', '35909.47', '31348.65', '**42079.94**', '39042.01', '38517.36', '37499.65', '34772.7', '15510.6']

Upvotes: 0

Anton vBR
Anton vBR

Reputation: 18916

How about putting them in a dictionary?

list_ = [30.3125, 13.75, 12.1875, 30.625, 18.125, 58.75, 38.125, 33.125, 55.3125, 28.75, 60.3125, 31.5625, 59.0625]
total = 150.0

dict_ = {}

sum_ = 0
i = 0

for item in list_:    
    # When sum + item > total reset sum and go to next key
    sum_ += item
    if sum_ + item > total:
        sum_ = 0
        i+= 1
    dict_.setdefault(i, []).append(item)

dict_

Prints

 {0: [30.3125, 13.75, 12.1875, 30.625, 18.125],
 1: [58.75, 38.125, 33.125],
 2: [55.3125, 28.75, 60.3125],
 3: [31.5625, 59.0625]}

And if you badly want the assignments you can do:

for key,value in dict_.items():
    if key == 0:
        exec("list_={}".format(str(value)))
    elif key == 1:
        exec("new_list={}".format(str(value)))
    else:
        exec("new_list{}={}".format(str(key-1),str(value)))

list_

Prints

[30.3125, 13.75, 12.1875, 30.625, 18.125]

Upvotes: 2

Related Questions