Combinatorial of elements in Python?
Wildemar Wildenburger
wildemar at freakmail.de
Wed Aug 15 08:09:53 EDT 2007
Sebastian Bassi wrote:
> I have 2 (or more) groups of elements, and I want to get all possible
> unique combinations from all of them. Is there a build-in method to do
> it?
>
> ADictionary={"one":["A","B","C","D"],"two":["H","I"]}
>
> I want to have all possible combinations from "one" and "two", that is:
>
> AH
> BI
> CH
> DI
> AI
> BH
> CI
> DH
>
> Sounds easy, but is not :)
>
>
Oh but it is:
>>> ADictionary={"one":["A","B","C","D"],"two":["H","I"]}
>>> result = set()
>>> for one in ADictionary["one"]:
... for two in ADictionary["two"]:
... result.add(one + two)
...
>>> result
set(['CI', 'CH', 'DH', 'DI', 'AI', 'AH', 'BH', 'BI'])
/W
More information about the Python-list
mailing list