python 排列组合之itertools
更新时间:2013年03月20日 20:53:51 作者:
python 排列组合之itertools,需要的朋友可以参考一下
python 2.6 引入了itertools模块,使得排列组合的实现非常简单:
import itertools
有序排列:e.g., 4个数内选2个排列:
>>> print list(itertools.permutations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
无序组合:e.g.,4个数内选2个:
>>> print list(itertools.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
复制代码 代码如下:
import itertools
有序排列:e.g., 4个数内选2个排列:
复制代码 代码如下:
>>> print list(itertools.permutations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
无序组合:e.g.,4个数内选2个:
复制代码 代码如下:
>>> print list(itertools.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
相关文章
Python opencv实现人眼/人脸识别以及实时打码处理
这篇文章主要为大家详细介绍了Python opencv实现人眼、人脸识别,以及实时打码处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-04-04


最新评论