Created
May 5, 2020 14:47
-
-
Save dkdndes/f59d3a8dab475dce29272d9bee912d73 to your computer and use it in GitHub Desktop.
This file contains hidden or 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