Permutations of a list of length n in python

1.5k Views Asked by At

So i started learning python and thought for an exercise I would try to write up a little script just to see if I could. Turns out I couldn't get it just right and would have left it, but got a little determined and now have a vendetta against this particular function.

I'm wanting to get the code to take a raw input of a given number and from that generate all possible permutations of a list of the numbers up to it. eg. if the input was "5" then it would generate all permutations of length 5 for [1, 2, 3, 4, 5].

What I tried was as follows:

from itertools import permutations
from math import factorial

n = raw_input("Input number to generate permutation list")

factorial_func = factorial(n)

print "there are %s permutations as follows:" %(factorial_func)

print list(permutations([1:n], n))

I know the faulty line is line 10 because of the [1:n] part and i don't know how to get it to make a list from 1 to n and put that into the permutation function. (I was hoping by going [1:n] it would generate a list from 1 to n in the same way that you can use it to access parts of a list from a to b with list_name[a:b] but it seems that isn't the case)

Sorry if this seems really trivial or is an obvious mistake, I only just started trying to learn python a few days ago.

2

There are 2 best solutions below

2
On

Yeah that is the bad line. What you are doing with the [1:n] is called slicing and is completely unrelated to coming up with ranges. Use the range function instead:

range(1, n+1)

(Note the n+1. Range is not inclusive of the end number).

You also aren't taking input properly. raw_input give a string, and you want an int so do:

n = int(raw_input("Input number to generate permutation list"))
0
On

In referring to Python docs about permutations (which you should make it as your primary reference on how to use module functions):

itertools.permutations(iterable[, r])

Return successive r length permutations of elements in the iterable.

If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Now in your code, you are not passing the correct arguments to the permutations function, [1:n] is a slicing and even it is not complete, it should be slicing of some list, like for example:

l = range(4)
print l[1:4] #Slice of list l

Another thing, when you read user input using raw_input, you have to convert it to integer if that's what you expect from the user or just use input which will directly return to you integer value of user input.

Finally, if you want a list of integers in the range of [1, n+1], use range function, this way: range(1,n+1)

so Fixing that will be like:

from itertools import permutations
from math import factorial

n = int(raw_input("Input number to generate permutation list"))
#or n = input("Input number to generate permutation list")

factorial_func = factorial(n)

print "there are %s permutations as follows:" %(factorial_func)

print list(permutations(range(1,n+1))