[英]Genereating all possible combinations of array (without repetition) in python
您将如何获得以下数组的所有可能组合?
array=[1,2,3]
示例
可能的组合是
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2
使用itertools
模块特别是permutation
:
from itertools import permutations
print(list(permutations([1, 2, 3])))
Python 很简单,因为有很多可用的库。 浏览源代码文档https://docs.python.org/3/library/itertools.html#itertools.permutations
import itertools
a=[1,2,3]
for k in itertools.permutations(a, len(a)):
print(k)
解决方案
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.