您的位置:首页 > 编程语言 > Python开发

Python 模块 itertools

2015-03-16 21:01 183 查看
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)]


count

原型:

count(start[, step])
返回:

start, start+step, start+2*step, ... (以start为首项, step为公差的等差数列)

example:

count(10):10, 11, 12, 13, .....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: