1

I have list of dictionaries in python and another variable specifying total amount of items to split between first list items.

listA= [ {'name': 'one', 'priority': 3 }, {'name': 'two', 'priority': 6 }, {'name': 'three', 'priority': 1 }  ]

items_to_split= 20

I'm trying to find a way how to assign appropriate amount of items from items_to_split to items from listA based on the priority.Count of items in first list might is various. The higher item priority is the more items from 20 should he get. In this example I'm looking for result like this: (example case is ideal because there is no remain after division)

one: 6 two: 12 three: 2
or
one one one one one one two two two two two two two two two two two two three three
I calculated weight per one priority point - 20/(3+6+1) = 2, and then multiplying the priority of item with that number.
Is there a way how to do it smarted in python using queues or some other method ? thank you

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
etharendil
  • 83
  • 1
  • 3
  • 7
  • Can you add a non-ideal example, as I am not sure the result you expect in this case. – FunkySayu Sep 11 '17 at 11:17
  • non ideal example would be when there is rest after items_to_split division by total amount of the priority e.g. listA= [ {'name': 'one', 'priority': 4 }, {'name': 'two', 'priority': 6 }, {'name': 'three', 'priority': 2 } ] items_to_split= 20 4+6+2=12 20/12=1,666 one 4*1,66 = 6.64 two = 6*1,66 = 9.96 three = 2*1,66 = 3.63 I can split only whole number so I'm not sure how to assign that properly – etharendil Sep 11 '17 at 11:47
  • So taking the integer part of this should solve your issue right (e.g. `int(4*1.66) = 6 "ones"`? Are you looking for a specific datastructure to handle this or an algorithm solving this case? – FunkySayu Sep 11 '17 at 12:02
  • the result data structure might look similar as listA e.g. [ {'name': 'one', 'count': 3 }, {'name': 'two', 'count': 6 }, {'name': 'three', 'count': 1 } ] where count is the result amount. but int(4*1.66) is 6 and the remain 0,64 would be "unused" so at the end, after iterating over all listA items we might not have 20 but e.g. 18. – etharendil Sep 11 '17 at 12:36
  • Read about [Modulo](https://stackoverflow.com/a/4432235/7414759), for your case `if modulo > (division / 2): int(division) + 1`. – stovfl Sep 11 '17 at 19:31

0 Answers0