Last active
March 11, 2016 11:17
-
-
Save minstrel271/d51654af3fa4e6411267 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
from itertools import product | |
def named_product(**kwargs): | |
""" | |
Cartesian product of input iterables with keys. | |
Due to how python handle kwargs, the order of the keys is not conserved. | |
>>> dict(x=1, y='a') in list(named_product(x=[1, 2], y=['a', 'b'])) | |
True | |
""" | |
yield from (dict(zip(kwargs.keys(), prod)) | |
for prod in product(*kwargs.values())) | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment