py itertools?

mattia gervaz at gmail.com
Sat Dec 19 05:54:58 EST 2009


Hi all, I need to create the permutation of two strings but without 
repeat the values, e.g. 'ab' for me is equal to 'ba'. Here is my 
solution, but maybe the python library provides something better:

>>> def mcd(a, b):
...     if b == 0:
...         return a
...     else:
...         return mcd(b, a % b)
...
>>> def mcm(a, b):
...     return int((a * b) / mcd(a, b))
...
>>> s1 = 'abc'
>>> s2 = 'wt'
>>> m = mcm(len(s1), len(s2))
>>> set(zip(s1*m, s2*m))
{('a', 'w'), ('a', 't'), ('b', 'w'), ('c', 't'), ('b', 't'), ('c', 'w')}

Any help?

Thanks, Mattia



More information about the Python-list mailing list