简体   繁体   English

python中长度为n的列表的排列

[英]Permutations of a list of length n in python

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. 因此,我开始学习python,并考虑进行练习,我会尝试编写一个小脚本以查看是否可以。 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]. 如果输入为“ 5”,则它将为[1、2、3、4、5]生成长度为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. 我知道错误的行是第10行,因为有[1:n]部分,我不知道如何获取它以列出从1到n的列表并将其放入置换函数。 (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) (我希望通过[1:n]可以生成一个从1到n的列表,就像您可以使用它使用list_name [a:b]访问列表中从a到b的部分一样,但是似乎并非如此)

Sorry if this seems really trivial or is an obvious mistake, I only just started trying to learn python a few days ago. 抱歉,如果这看起来确实是微不足道的,或者是一个明显的错误,几天前我才刚开始尝试学习python。

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. 您使用[1:n]操作称为切片,并且与提出范围完全无关。 Use the range function instead: 使用range函数代替:

range(1, n+1)

(Note the n+1 . Range is not inclusive of the end number). (请注意, n+1范围不包括结束号)。

You also aren't taking input properly. 您也没有正确输入信息。 raw_input give a string, and you want an int so do: raw_input提供一个字符串,并且您想要一个int,这样:

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

In referring to Python docs about permutations (which you should make it as your primary reference on how to use module functions): 在参考有关排列的 Python文档时(您应该将其作为如何使用模块函数的主要参考):

itertools.permutations(iterable[, r]) itertools.permutations(iterable [,r])

Return successive r length permutations of elements in the iterable. 返回迭代器中元素的连续r长度排列。

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. 如果未指定r或为None,则r默认为可迭代的长度,并生成所有可能的全长置换。

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: 现在,在您的代码中,您没有将正确的参数传递给permutations函数, [1:n]是切片,甚至不完整,它应该是某些列表的切片,例如:

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. 另一件事,当你使用读取用户输入raw_input ,你必须把它转换为整数。如果这是你从用户想得到什么,或者只是使用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) 最后,如果要在[1,n + 1]范围内的整数列表,请使用range函数,方法是: 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM