当前位置: 移动技术网 > IT编程>脚本编程>Python > python 排列组合之itertools

python 排列组合之itertools

2019年04月04日  | 移动技术网IT编程  | 我要评论

assemblyinfo.cs,微型摆线针轮减速机,开讲啦 李连杰

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)]

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网