Last active
December 21, 2015 13:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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