py itertools?

Lie Ryan lie.1296 at gmail.com
Sat Dec 19 09:52:17 EST 2009


On 12/19/2009 11:48 PM, Chris Rebert wrote:
>
> Surprised you didn't think of the seemingly obvious approach:
>
> def permute_chars(one, two):
>      for left in set(one):
>          for right in set(two):
>              yield (left, right)
>
>>>> list(permute_chars('abc', 'wt'))
> [('a', 'w'), ('a', 't'), ('b', 'w'), ('b', 't'), ('c', 'w'), ('c', 't')]
>

even less work:
import itertools
print set(itertools.product('abc', 'wt'))

but neither of those two solves the OP's problem. And neither the OP's 
own solution solves his own problem (per my understanding from his 
description).

what he wanted was something like:
print set(tuple(sorted(x)) for x in itertools.product(s1, s2))


or, just for some functional fun, when written in point-free form:

from itertools import product
from functools import partial
def compose(f, g):
     return lambda *a, **k: f(g(*a, **k))

sortedtuple = compose(tuple, sorted)
setcomp = compose(set, map)
unique_tuples = partial(setcomp, sortedtuple)
permute_chars = compose(unique_tuples, product)
print permute_chars(s1, s2)



More information about the Python-list mailing list