Last active
August 29, 2015 14:24
-
-
Save WayneSan/3b1b4bb593a7c259263e to your computer and use it in GitHub Desktop.
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
# users = queryset.values_list('group', 'username').distinct() | |
users = [('admin', 'root'), ('group1', 'andy'), ('group1', 'tim')] | |
grouped_users = reduce(lambda d, (k, v): dict(d, **{k: d.get(k, [])+[v]}), users, {}) | |
print grouped_users # {'admin': ['root'], 'group1': ['andy', 'tim']} | |
choices = map(lambda (k, v): (k, [('%s_%s' % (k, x), x) for x in v]), grouped_users.items()) | |
print choices # [('admin', [('admin_root', 'root')]), ('group1', [('group1_andy', 'andy'), ('group1_tim', 'tim')])] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment