Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中前几个整数的置换_Python_Permutation - Fatal编程技术网

Python中前几个整数的置换

Python中前几个整数的置换,python,permutation,Python,Permutation,我想在Python中遍历(1,2,3,4,5)的所有排列,并为每个排列计算一些东西,如 for li in PermutationLists: # PermutationLists = [[1,2,3,4,5],[1,2,3,5,4],...] print li[0]-li[1]+li[2]-li[3]+li[4] 什么是迭代(1,2,3,4,5)所有这些排列的方便方法?正如Ashwini Chaudhary在评论中指出的那样: from itertools import permut

我想在Python中遍历(1,2,3,4,5)的所有排列,并为每个排列计算一些东西,如

for li in PermutationLists: # PermutationLists = [[1,2,3,4,5],[1,2,3,5,4],...]
    print li[0]-li[1]+li[2]-li[3]+li[4]

什么是迭代(1,2,3,4,5)所有这些排列的方便方法?

正如Ashwini Chaudhary在评论中指出的那样:

from itertools import permutations

permutes = permutations([1,2,3,4,5])

for li in permutes:
    # do stuff

itertools.permutation
?为什么要将permutations对象存储在变量中?为什么不简单地重复它呢?没有特别的原因