Python中前几个整数的排列

时间:2014-02-18 03:02:36

标签: 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)的所有这些排列的方便方法是什么?

1 个答案:

答案 0 :(得分:2)

Ashwini Chaudhary在评论中指出:

from itertools import permutations

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

for li in permutes:
    # do stuff