itertools 对偶数个变量的排列

itertools permutation on oneven amount of variables

我有这个公式

x*A + y*B + z*C

x,y,z 是变量

A,B,C 为常量

我想对该公式应用所有可能的排列并存储每个结果

使用偶数个排列,这相当简单

from itertools import permutations 

perm = permutations([1, 2,3])

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]



#  ["1 * A " + 2 * B + 3 * C],   -->append
  #  ["1 * A " + 3 * B + 2 * C],  --->append 
# ............

如何将其应用于不均匀排列数组,例如 perm = permutations([1, 2,3,4,5])

itertools.permutations 有一个可选的第二个参数,可让您选择每个排列的大小:

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

结果:

[(1, 2, 3), (1, 2, 4), (1, 2, 5)...]