Question regarding Higher-Order-Programming in Python
Arnaud Delobelle
arnodel at gmail.com
Wed Dec 22 16:45:12 EST 2010
Mark Fink <mark at mark-fink.de> writes:
> so far I have never noticed chain.from_iterable, but many thanks to
> you Peter, I have now a beautiful solution to this problem.
>>>> from itertools import chain
>>>> comb = it.combinations(dims, 2)
>>>> l = chain.from_iterable(it.imap(get_products, comb))
You can also write this as:
l = (p for c in comb for p in get_products(c))
>>>> l.next()
> [('special', '+'), ('number', 1)]
>>>> l.next()
> [('special', '+'), ('number', 2)]
Also in your original post you define get_products:
>>>> def get_products(keys):
> ... # helper to get products from keys in the following form:
> ... # [('bold', True), ('color', 'black')]
> ... values = itemgetter(*keys)(dims)
> ... product = it.product(*values)
> ... return map(partial(zip, keys), product)
> ...
You could define it as e.g.
def get_products(keys):
key_values = [[(k, v) for v in values] for k in keys]
return it.product(*key_values)
But maybe for some reason you are trying to avoid list comprehensions
and generator expressions?
--
Arnaud
More information about the Python-list
mailing list