python-itertools排列组合模块

以下函数均返回迭代器

count

原型:

count(start[, step])

返回:

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

example:

count(10):10, 11, 12, 13, .....

cycle

原型:

cycle(p)

返回:

p0, p1, p2, ...,plast, p0, p1, ... (对序列循环遍历)

repeat

原型

repeat(elem[, n])

返回:

elem, elem, elem...( 重复n次或者无限下去)

chain

原型:

chain(p, q, ...)

返回:

p0, p1, ..., plast, q0, q1, ..., qlast, ... (一个一个地遍历这些序列的, 不必生成大序列即可这样遍历)

compress

原型:

compress(data d, selector s)

返回:

(d[0] if s[0]), (d[1] if s[1]), ...

例子:

from itertools import *
for i in compress("abcdef", [1, 1, 0, 1, 0, 1]): # a, b, d, f
    print i

dropwhile

原型:

dropwhile(func f, seq q)

返回:

当函数f执行返回假时, 开始迭代序列

例子:

from itertools import *
for i in dropwhile(lambda x:x<5, [1, 2, 6, 4, 3]):#6, 4, 3
    print i

groupby

原型:

groupby(iterable[, keyfunc])

返回:按照keyfunc函数对序列每个元素执行后的结果分组(每个分组是一个迭代器), 返回这些分组的迭代器

例子:

from itertools import *
a = ['aa', 'ab', 'abc', 'bcd', 'abcde']
for i, k in groupby(a, len):#按照字符串的长度对a的每个元素进行分组
    for m in k:
        print m, 
    print i

输出:

aa ab 2
abc bcd 3
abcde 5

ifilter

原型:

ifilter(func, seq)

返回:对函数func执行返回真的元素的迭代器

例子:

from itertools import *
a = [1, 2, 3, 4, 5]
for i in ifilter(lambda x:x>3, a):#4, 5
    print i

ifilterfalse

原型:

ifilterfalse(func, seq)

返回:对函数func执行返回假的元素的迭代器

islice

原型:

islice(seq[, start], stop[, step])

返回:返回序列seq的从start开始到stop结束的步长为step的元素的迭代器

from itertools import *
for i in islice("abcdef", 0, 4, 2):#a, c
    print i

imap

原型:

imap(func, seq)

返回 :返回序列每个元素被func执行后返回值的序列的迭代器

from itertools import *
for i in imap(lambda x:x+1, range(10)):#1, 2, ..., 10
    print i

starup

原型:

starup(func, seq)

返回:对序列seq的每个元素作为func的参数列表执行, 返回执行结果的迭代器

例子:

from itertools import *
for i in starmap(pow, [(2,2), (3, 2)]):#4, 9
    print i

tee

原型:

tee(it[, n = 2])

把一个迭代器分为n个迭代器, 返回一个元组.默认是两个

from itertools import *
a = "hello"
c, d = tee(iter(a), 2)
for i, j in zip(c, d):
    print i, j

输出:

h h
e e
l l
l l
o o

takewhile

原型:

takewhile(func, seq)

从序列的头开始, 直到执行函数func失败.

例子:

from itertools import *
for i in takewhile(lambda x:x<6, range(10)):#1, 2, 3, 4, 5
    print i

izip

雷同于zip

原型:

izip(iter1, iter2, ... iterN):

返回:(it1[0],it2 [0], it3[0], ..), (it1[1], it2[1], it3[1], ..)...

只要提供的某个迭代器不再生成值,迭代就会停止

izip_longest

原型同izip

只不过, 迭代过程会持续到所有输入迭代变量iter1,iter2等都耗尽为止,如果没有使用fillvalue关键字参数指定不同的值,则使用None来填充已经使用的迭代变量的值

permutations

原型:

permutations(p[, r])

返回p中任意取r个元素做排列的元组的迭代器

实例:

from itertools import *
for i in permutations([1, 2, 3], 3):
    print i

输出:

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

product

原型:

product(iter1, iter2, ... iterN, [repeat=1])

创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数

from itertools import *
for i in product([1, 2, 3], [4, 5], [6, 7]):
    print i

输出:

(1, 4, 6)
(1, 4, 7)
(1, 5, 6)
(1, 5, 7)
(2, 4, 6)
(2, 4, 7)
(2, 5, 6)
(2, 5, 7)
(3, 4, 6)
(3, 4, 7)
(3, 5, 6)
(3, 5, 7)

combinations()

原型

combinations(iterable, r)

创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序

note:不带重复

例子:

from itertools import *

for i in combinations([1, 2, 3], 2):
    print i

输出:

(1, 2)
(1, 3)
(2, 3)

combinations_with_replacement

同上, 带重复 例子:

from itertools import *

for i in combinations_with_replacement([1, 2, 3], 2):
    print i

输出:

(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值