[SciPy-user] iterating through permutations

Alan G Isaac aisaac at american.edu
Thu Apr 6 20:21:22 CDT 2006


On Thu, 06 Apr 2006, Jonathan Taylor apparently wrote: 
> just wondering -- is there any easy way to iterate over 
> all permutations of, say, K integers in scipy? 

At http://www.american.edu/econ/pytrix/pytrix.py find the 
below.

Cheers,
Alan Isaac


def permuteg(lst):
    '''Return generator of all permutations of a list.

    :type `lst`: sequence
    :rtype:      list of lists
    :return:     all permutations of `lst`
    :requires:   Python 2.4+
    :note:       recursive
    :since:      2005-06-20
    '''
    return ([lst[i]]+x 
                    for i in range(len(lst))
                    for x in permute(lst[:i]+lst[i+1:])) \
                    or [[]]




More information about the SciPy-user mailing list