find permutations of items in a python list, with an added complexity

Please bear with me while I struggle to explain this; my math is rusty and I just started computer programming, sorry!

Say I have a list of 3 items. I want to find all possible arrangements of the items in this list where each arrangement consists of 3 items.

Next, still using my original list, I want to find all the possible arrangements of the items of the list, except I only want the arrangements to consist of two items.

Finally, I want to do the same thing again, except arrangements only consist of one item.

So I expect 3! + 3!/1! + 3!/2!, or 15 total arrangements. Just to be really explicit about what I want, if my list were [1, 2, 3], then the code should produce:

1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 2
3, 1, 2
3, 2, 1

1, 2
1, 3
2, 1
2, 3
3, 1
3, 2

1
2
3

The code I have written below can do what I have written above, but only for lists of length 3. I could modify the code to handle lists of greater length by adding extra 'for' loops and 'elif' statements, but I feel like there has to be a way to generalize the pattern. What should I do so that I can get permutations of the kind described above for lists of any length?

I think my exhaustive enumeration method might be making this more complicated than it needs to be... will try to think about other methods and update if solution found.

def helperFunction(itemsList):

    fullPermutationsOutputList = []


    def fullPermutations(itemsList, iterations):

        for item1 in itemsList:
            if iterations == 2:
                if len([item1]) == len(set([item1])):
                    fullPermutationsOutputList.append((item1,))
            else:    
                for item2 in itemsList:
                    if iterations == 1:
                        if len([item1, item2]) == len(set([item1, item2])):
                            fullPermutationsOutputList.append((item1, item2))
                    else:
                        for item3 in itemsList:
                            if iterations == 0:
                                if len([item1, item2, item3]) == len(set([item1, item2, item3])):
                                    fullPermutationsOutputList.append((item1, item2, item3))

        if iterations == 0:                        
            fullPermutations(itemsList, iterations + 1)
        elif iterations == 1:
            fullPermutations(itemsList, iterations + 1)

    fullPermutations(itemsList, 0)
    return fullPermutationsOutputList

Answers


Just itertools.permutations. You can inspect its sources if you want exact algo.


this will do what u want: https://stackoverflow.com/a/10784693/1419494

def perm(list_to_perm,perm_l,items,out):
            if len(perm_l) == items:
                out +=[perm_l]
            else:
                for i in list_to_perm:
                    if i not in perm_l:
                        perm(list_to_perm,perm_l +[i],items,out)


a = [1,2,3]
for i in range(1,len(a) +1):
    out = []
    perm(a,[],i,out)
    print out

Need Your Help

Sort Array from min to max

c# arrays sorting random

I have an array with random elements. there are 10 element in the array.

CSharpCodeProvider Compilation Performance

c# performance compiler-construction

Is CompileAssemblyFromDom faster than CompileAssemblyFromSource?