Skip to content

Instantly share code, notes, and snippets.

@SavvyGuard
Last active December 21, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavvyGuard/6314132 to your computer and use it in GitHub Desktop.
Save SavvyGuard/6314132 to your computer and use it in GitHub Desktop.
Evenly assign a list of values to another list of keys so that the each key has almost same total values assigned to it
# values are highest first
def assignValuesToKeysEvenly(keys, values):
""" keys are a list containing key names
values are a list of values
"""
keynames = {}
# make keynames a dict of keynames containing a list of assigned values
for key in keys:
keynames[key] = []
keycount = len(keys)
for valueindex, value in enumerate(values):
index = valueindex % (2 * keycount)
# don't do a simple loop, instead make it work like the NBA draft
if (index > (keycount - 1)):
index = 2 * keycount - index - 1
keyname = keys[index]
keynames[keyname].append(value)
return keynames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment