简体   繁体   中英

How to group data to have approximately the same summation regardless number of member

How to make summation of a group to be almost identical regardless number of member per group

ie.

input -> ('a', 100), ('b', 200), ('c', 300), ('d', 50), ('e', 200), ('f', 50)

output

group A ->  (a,100) , (b,200)  
group B ->  (c,300)            
group C ->  (d,50) , (e,200), (f,50)

each group got total sum of 300

a = [('a', 100), ('b', 200)]
b = [('c', 300)]
c = [('d', 50), ('e', 200), ('f', 50)]

group = []
for i in [a, b, c]:
    k = 0
    for j in i:
        k += j[1]
    group.append(k)
# or
group = [sum([j[1] for j in i]) for i in [a, b, c]]
print(group)

It shows you how many number of member in each group, if it different you can change you data. It would be better if you provider your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM