首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python标准库itertools中函数精要

Python标准库itertools中函数精要

作者头像
Python小屋屋主
发布2018-04-16 14:21:00
7390
发布2018-04-16 14:21:00
举报
文章被收录于专栏:Python小屋Python小屋

1、count()

>>> import itertools

>>> x = itertools.count(3)

>>> x

count(3)

>>> for i in range(10):

print(next(x), end=',')

3,4,5,6,7,8,9,10,11,12,

>>> x = itertools.count(3,5)

>>> x

count(3, 5)

>>> for i in range(10):

print(next(x), end=',')

3,8,13,18,23,28,33,38,43,48,

>>> for i in range(20):

print(next(x), end=',')

53,58,63,68,73,78,83,88,93,98,103,108,113,118,123,128,133,138,143,148,

2、cycle()

>>> x = 'abcdefg'

>>> y = itertools.cycle(x)

>>> y

<itertools.cycle object at 0x00000000031F5888>

>>> for i in range(20):

print(next(y),end=',')

a,b,c,d,e,f,g,a,b,c,d,e,f,g,a,b,c,d,e,f,

>>> for i in range(10):

print(next(y),end=',')

g,a,b,c,d,e,f,g,a,b,

>>> import itertools

>>> x = 'abcdefg'

>>> y = itertools.cycle(x)

>>> i = 0

>>> for item in y:

print(item, end=',')

i = i+1

if i>25:

break

a,b,c,d,e,f,g,a,b,c,d,e,f,g,a,b,c,d,e,f,g,a,b,c,d,e,

>>> next(y)

'f'

>>> next(y)

'g'

3、repeat()

>>> x = itertools.repeat(3)

>>> for i in range(10):

print(next(x), end=',')

3,3,3,3,3,3,3,3,3,3,

>>> for i in range(20):

print(next(x), end=',')

3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,

>>> x = itertools.repeat(3,5)

>>> list(x)

[3, 3, 3, 3, 3]

4、chain() 将多个序列转换为一个序列

>>> x = list(range(5))

>>> x

[0, 1, 2, 3, 4]

>>> y = range(5,10)

>>> itertools.chain(x, y)

<itertools.chain object at 0x00000000031E9BA8>

>>> list(_)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> z = ('a', 'b', 'c')

>>> list(itertools.chain(x, y, z))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c']

5、compress() 根据一个序列的值对另一个序列进行过滤

>>> x = range(1, 20)

>>> x

range(1, 20)

>>> list(x)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> y = (1,0)*9+(1,)

>>> y

(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1)

>>> list(itertools.compress(x, y))

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

6、groupby() 根据函数返回值进行分类

>>> def group(v):

if v>10:

return 'greater than 10'

elif v<5:

return 'less than 5'

else:

return 'between 5 and 10'

>>> x = range(20)

>>> x

range(0, 20)

>>> list(x)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> y = itertools.groupby(x, group)

>>> for k, v in y:

print(k, ':', list(v))

less than 5 : [0, 1, 2, 3, 4]

between 5 and 10 : [5, 6, 7, 8, 9, 10]

greater than 10 : [11, 12, 13, 14, 15, 16, 17, 18, 19]

7、zip_longest()

>>> x = range(5)

>>> y = range(8)

>>> list(zip(x, y))

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

>>> list(itertools.zip_longest(x, y, fillvalue='*'))

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), ('*', 5), ('*', 6), ('*', 7)]

>>> list(itertools.zip_longest(x, y, fillvalue='-'))

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), ('-', 5), ('-', 6), ('-', 7)]

>>> list(itertools.zip_longest(x, y, fillvalue=9))

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (9, 5), (9, 6), (9, 7)]

8、product() 笛卡尔积

>>> itertools.product([1,2,3],[4,5,6])

<itertools.product object at 0x00000000031FA870>

>>> list(_)

[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]

>>> list(itertools.product([1,2,3],[4,5],[6,7]))

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

>>> list(itertools.product([1,2],[4,5], repeat=3))

[(1, 4, 1, 4, 1, 4), (1, 4, 1, 4, 1, 5), (1, 4, 1, 4, 2, 4), (1, 4, 1, 4, 2, 5), (1, 4, 1, 5, 1, 4), (1, 4, 1, 5, 1, 5), (1, 4, 1, 5, 2, 4), (1, 4, 1, 5, 2, 5), (1, 4, 2, 4, 1, 4), (1, 4, 2, 4, 1, 5), (1, 4, 2, 4, 2, 4), (1, 4, 2, 4, 2, 5), (1, 4, 2, 5, 1, 4), (1, 4, 2, 5, 1, 5), (1, 4, 2, 5, 2, 4), (1, 4, 2, 5, 2, 5), (1, 5, 1, 4, 1, 4), (1, 5, 1, 4, 1, 5), (1, 5, 1, 4, 2, 4), (1, 5, 1, 4, 2, 5), (1, 5, 1, 5, 1, 4), (1, 5, 1, 5, 1, 5), (1, 5, 1, 5, 2, 4), (1, 5, 1, 5, 2, 5), (1, 5, 2, 4, 1, 4), (1, 5, 2, 4, 1, 5), (1, 5, 2, 4, 2, 4), (1, 5, 2, 4, 2, 5), (1, 5, 2, 5, 1, 4), (1, 5, 2, 5, 1, 5), (1, 5, 2, 5, 2, 4), (1, 5, 2, 5, 2, 5), (2, 4, 1, 4, 1, 4), (2, 4, 1, 4, 1, 5), (2, 4, 1, 4, 2, 4), (2, 4, 1, 4, 2, 5), (2, 4, 1, 5, 1, 4), (2, 4, 1, 5, 1, 5), (2, 4, 1, 5, 2, 4), (2, 4, 1, 5, 2, 5), (2, 4, 2, 4, 1, 4), (2, 4, 2, 4, 1, 5), (2, 4, 2, 4, 2, 4), (2, 4, 2, 4, 2, 5), (2, 4, 2, 5, 1, 4), (2, 4, 2, 5, 1, 5), (2, 4, 2, 5, 2, 4), (2, 4, 2, 5, 2, 5), (2, 5, 1, 4, 1, 4), (2, 5, 1, 4, 1, 5), (2, 5, 1, 4, 2, 4), (2, 5, 1, 4, 2, 5), (2, 5, 1, 5, 1, 4), (2, 5, 1, 5, 1, 5), (2, 5, 1, 5, 2, 4), (2, 5, 1, 5, 2, 5), (2, 5, 2, 4, 1, 4), (2, 5, 2, 4, 1, 5), (2, 5, 2, 4, 2, 4), (2, 5, 2, 4, 2, 5), (2, 5, 2, 5, 1, 4), (2, 5, 2, 5, 1, 5), (2, 5, 2, 5, 2, 4), (2, 5, 2, 5, 2, 5)]

9、permutations()、combinations() 排列、组合

>>> list(itertools.permutations([1,2,3,4,5], 3))

[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 2), (1, 3, 4), (1, 3, 5), (1, 4, 2), (1, 4, 3), (1, 4, 5), (1, 5, 2), (1, 5, 3), (1, 5, 4), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 3, 1), (2, 3, 4), (2, 3, 5), (2, 4, 1), (2, 4, 3), (2, 4, 5), (2, 5, 1), (2, 5, 3), (2, 5, 4), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2, 1), (3, 2, 4), (3, 2, 5), (3, 4, 1), (3, 4, 2), (3, 4, 5), (3, 5, 1), (3, 5, 2), (3, 5, 4), (4, 1, 2), (4, 1, 3), (4, 1, 5), (4, 2, 1), (4, 2, 3), (4, 2, 5), (4, 3, 1), (4, 3, 2), (4, 3, 5), (4, 5, 1), (4, 5, 2), (4, 5, 3), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 2, 1), (5, 2, 3), (5, 2, 4), (5, 3, 1), (5, 3, 2), (5, 3, 4), (5, 4, 1), (5, 4, 2), (5, 4, 3)]

>>> list(itertools.permutations([1,2,3,4,5], 2))

[(1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4)]

>>> list(itertools.permutations([1,2,3,4], 4))

[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]

>>> x = itertools.permutations([1,2,3,4], 4)

>>> next(x)

(1, 2, 3, 4)

>>> next(x)

(1, 2, 4, 3)

>>> next(x)

(1, 3, 2, 4)

>>> next(x)

(1, 3, 4, 2)

>>> x = itertools.combinations([1,2,3,4], 4)

>>> list(x)

[(1, 2, 3, 4)]

>>> list(itertools.combinations([1,2,3,4,5], 3))

[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]

10、accumulate()

>>> x = [1, 2, 3, 4, 5]

>>> itertools.accumulate(x)

<itertools.accumulate object at 0x0000000003181F08>

>>> list(itertools.accumulate(x))

[1, 3, 6, 10, 15]

>>> def sub(a, b):

return b-a

#后项减前项,即时生效 >>> list(itertools.accumulate(x, sub))

[1, 1, 2, 2, 3] >>> list(itertools.accumulate(x, lambda a,b: a*b))

[1, 2, 6, 24, 120]

11、takewhile()、dropwhile() 当条件满足时选取/丢弃,直至条件不成立。

>>> import random

>>> x = list(range(20))

>>> random.shuffle(x)

>>> x

[17, 3, 0, 13, 1, 4, 11, 9, 14, 16, 15, 18, 6, 7, 8, 19, 10, 5, 2, 12]

>>> list(itertools.takewhile(lambda x: x<18, x))

[17, 3, 0, 13, 1, 4, 11, 9, 14, 16, 15]

>>> list(itertools.takewhile(lambda x: x<19, x))

[17, 3, 0, 13, 1, 4, 11, 9, 14, 16, 15, 18, 6, 7, 8]

>>> list(itertools.dropwhile(lambda x: x<19, x))

[19, 10, 5, 2, 12]

>>> list(itertools.dropwhile(lambda x: x<18, x))

[18, 6, 7, 8, 19, 10, 5, 2, 12]

>>> list(itertools.dropwhile(lambda x: x<16, x))

[17, 3, 0, 13, 1, 4, 11, 9, 14, 16, 15, 18, 6, 7, 8, 19, 10, 5, 2, 12]

12、startmap()

功能说明:func(*seq[0]), func(*seq[1]), ...

>>> x = [(2, 5), (3, 2), (10, 3)]

>>> itertools.starmap(pow, x)

<itertools.starmap object at 0x0000000003187EB8>

>>> list(itertools.starmap(pow, x))

[32, 9, 1000]

>>> def add(a, b):return a+b

>>> list(itertools.starmap(add, x))

[7, 5, 13]

>>> list(itertools.starmap(lambda a,b: a-b, x))

[-3, 1, 7]

>>> list(itertools.starmap(lambda a,b: a*b, x))

[10, 6, 30]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-07-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档