python - How would you evenly distribute a list of sublists that each have a numerical value across three lists? -
How would you distribute the list of books that have the value of each?
I would like to distribute the following list in 3 lists.
lst = [['a', 10], ['b', 40], ['', '10'], ['d', 30], ['e' Since the sum of all values is 304, there are three lists: ['f', 100], ['g', 90], ['h', 4]] Should be approximately 101.3
The result of this is that I want to produce in some form. ['A', 10]] lst2 = [['F', 100], ['H', 4]] lst3 = [['G', 90] [['B', 40], ['C' , 10], ['D', 30], ['E', 20]]
This is my solution yet it shows, but to make it fast Work is needed. Def Lists (lst): lst = map (Lambda Subby: Subly [1], LCT) Return amount (LST) DEF Destination (LST): Reach the bucket until you reach #, then For the next bucket go ahead Lst1 = [] Lst2 = [] Lst3 = [] For Suble lst: try: if ListSum (Lst1) & lt; 100: Lst1.append (Subly) Eleph Listings (Lst2) & lt; 100: Lst2.append (subli) Other: Except Lst3.append (subli): Pass Print Lst1 Print Lst2 Print Lst3
This is a simple implementation First of all, weigh the input list by weight, then tell each item by adding it to the full bucket.
DEF inner dentist (LST, N): Distribute items in "Vale, Weight") "Bucket" in N Bucket = [I] = [[0, i] Sorted in item for category (n)] (lst, key = lambda X: x [1], reverse = true): idx = weight [0] [1] bucket [idx ] .Appendix (item) weight [0] [0] + = item [1] weight = sorted (weight) return bucket so
returns
[['F', 100], ['H', 4]] [['' G '', 90] ], ['A', 10]] [['B', 40], ['D', 30], ['E', 20], ['C', 10]]
Comments
Post a Comment