from itertools import permutations
print(list(permutations(range(1,6))))
这仅给出 5 位数字,但我也想要 3 位和 4 位数字。此代码的输出类似于 12543、45321 等。所有这些都只给出了 5 位数字。我想要的输出是 123、312、4321,以及 23415 等。我想要所有可能的 3 位、4 位和 5 位数字
存在一个r
参数,例如itertools.permutations([1,2,3], r=2)
,它将生成所有可能的排列选择 2 个元素:[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句