[SciPy-user] Finding all combinations of numbers

Alan G Isaac aisaac at american.edu
Sun Jan 13 09:44:40 EST 2008


Here's a powerset creator that should work for any iterable.
It is easily changed to a generator, which might be wise
if your iterables are of any length.

def subsets(s):
    result = list( [] )
    for xi in s:
        result += list( subset+[xi] for subset in result )
    return result

Cheers,
Alan Isaac





More information about the SciPy-User mailing list