Python中前几个整数的置换
我想在Python中遍历(1,2,3,4,5)的所有排列,并为每个排列计算一些东西,如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
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对象存储在变量中?为什么不简单地重复它呢?没有特别的原因