Created
June 21, 2013 06:49
-
-
Save lssergey/5829355 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
from itertools import product | |
def combinations(d): | |
""" | |
>>> d = {'a': [2, 5, 6], 'b': [8, 3, 6]} | |
>>> combinations(d) | |
[{'a': 2, 'b': 8}, {'a': 2, 'b': 3}, {'a': 2, 'b': 6}, {'a': 5, 'b': 8}, {'a': 5, 'b': 3}, {'a': 5, 'b': 6}, {'a': 6, 'b': 8}, {'a': 6, 'b': 3, {'a': 6, 'b': 6}] | |
""" | |
return [dict(combo) for combo in product(*[ | |
[(items[0], element) for element in items[1]] | |
for items in d.viewitems() | |
])] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Хорошо что работает, плохо что "невыразительно" - прочитав выражение после return один раз, не понимаешь сразу что возвращается. Требуется прочитать еще пару раз. У нас это называют "невыразительный код". Может быть я субъективен, может быть все дело в том как часто в ежедневной жизни используется этот самый product и насколько он ясен и привычен. Я вчера вечером, подумал о нем, перечитал его доку, но не сообразил как его применить в этом случае.