How to populate all possible hierarchical clusterings from a set of elements?

Peter Otten __peter__ at web.de
Fri Jan 14 02:05:31 EST 2011


Arnaud Delobelle wrote:

> more simply:
> 
> def clusters(l):
>     if len(l) == 1:
>         yield l[0]
>         return
>     for i in range(1, len(l)):
>         for left in clusters(l[:i]):
>             for right in clusters(l[i:]):
>                 yield (left, right)
> 
> That would give all solutions without duplicates.  In fact, this is
> simply finding all full binary trees which order l.

Easy, now that I see it ;)
 




More information about the Python-list mailing list