permutations - fast & with low memory consumption?

Alan Isaac aisaac at american.edu
Tue Dec 19 17:25:27 EST 2006


>From pytrix:
http://www.american.edu/econ/pytrix/pytrix.py

def permutationsg(lst):
    '''Return generator of all permutations of a list.
    '''
    if len(lst)>1:
        for i in range(len(lst)):
            for x in permutationsg(lst[:i]+lst[i+1:]):
                yield [lst[i]]+x
    else:
        yield lst

hth,
Alan Isaac





More information about the Python-list mailing list