python itertools 模块完全掌握(2)

1. 全部

  1. count(start=0, step=1)
  2. repeat(elem [,n])
  3. accumulate(p[, func])
  4. chain(p, q, …)
  5. chain.from_iterable([p, q, …])
  6. compress(data, selectors)
  7. dropwhile(pred, seq)
  8. groupby(iterable[, keyfunc])
  9. filterfalse(pred, seq)
  10. islice(seq, [start,] stop [, step])
  11. starmap(fun, seq)
  12. tee(it, n=2)
  13. takewhile(pred, seq)
  14. zip_longest(p, q, …)
  15. product(p, q, … [repeat=1])
  16. permutations(p[, r])
  17. combinations(p, r)
  18. combinations_with_replacement(p, r)
    本节主要介绍8-18,1-7见上一节内容

2. 详解

#!/usr/bin/env python
# encoding: utf-8


"""
@python version: python3.6.1
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@site: http://blog.csdn.net/github_36326955
@software: PyCharm
@file: suggest5.py
@time: 5/2/2017 5:58 PM
"""

import itertools


# ex2.5:groupby
# groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)

"""
for item in iterable, 我们看一下keyfunc(item)的返回值,将返回值相同的列为一组。name为每一组对应的返回值,
group为该组的成员
"""
qs = [{'date' : 1},{'date' : 2}]
[(name, list(group)) for name, group in itertools.groupby(qs, lambda p:p['date'])]
"""
lambda函数的输入p是qs的item, 返回值是qs每一项的'date'属性值。按照这个进行分组。
output:
[(1, [{'date': 1}]), (2, [{'date': 2}])]
"""

a = ['aa', 'ab', 'abc', 'bcd', 'abcde']
for i, k in itertools.groupby(a, len):
    print(i, list(k))
"""

output:
2 ['aa', 'ab']
3 ['abc', 'bcd']
5 ['abcde']
"""

# ex2.6: filterfalse
# filterfalse(pred, seq) --> elements of seq where pred(elem) is False
def pred(x):
    return x < 1
for x in itertools.filterfalse(pred, [-1,0,2,1,-3,2]):
    print(x,end=',')
"""
output:
2,1,2
"""

# ex2.7: islice
# islice(seq, [start,] stop [, step]) --> elements from
#       seq[start:stop:step]
for x in itertools.islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1, 8, 2):
    print(x, end=',')
"""
output:
1,3,5,7,
"""

# ex2.8: starmap
# starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...
values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
for i in itertools.starmap(lambda x, y: (x, y, x*y), values):
    print('%d * %d = %d' % i)

"""
output:
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
"""


# ex2.9: tee
# tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n
"""
tee(it,n)中,it为一个迭代器,通过tee操作,我们可以得到n个一样的迭代器:
"""
for x in itertools.tee([1,2,3,4,5,6],3):
    for i in x:
        print(i,end=",")
    print()
"""
output:
1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6,
"""


# ex2.10:takewhile
# takewhile(pred, seq) --> seq[0], seq[1], until pred fails
def pred(x):
    return x < 1

for x in itertools.takewhile(pred, [-1,-2,0,3,4,-1,2]):
    print(x,end=",")
"""
output:
-1,-2,0,
"""

# ex2.11:zip_longest
# zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ...
for x in itertools.zip_longest([0,1,2,3],[4,5],[6,7]):
    print(x, end=",")
"""
output:
(0, 4, 6),(1, 5, 7),(2, None, None),(3, None, None),
"""



# ex3: Combinatoric generators:
# ex3.1:product
# product(p, q, ... [repeat=1]) --> cartesian product
import itertools
a = (1, 2, 3)
b = ('A', 'B', 'C')
c = itertools.product(a,b,repeat=1)
d = itertools.product(a,b,repeat=2)
for elem in c:
    print(elem, end=",")
print()
for e in d:     # 在c得到的基础上,进行笛卡尔乘积
    print(e, end=",")
"""
output:
(1, 'A'),(1, 'B'),(1, 'C'),(2, 'A'),(2, 'B'),(2, 'C'),(3, 'A'),(3, 'B'),(3, 'C'),
(1, 'A', 1, 'A'),(1, 'A', 1, 'B'),(1, 'A', 1, 'C'),
(1, 'A', 2, 'A'),(1, 'A', 2, 'B'),(1, 'A', 2, 'C'),
(1, 'A', 3, 'A'),(1, 'A', 3, 'B'),(1, 'A', 3, 'C'),
(1, 'B', 1, 'A'),(1, 'B', 1, 'B'),(1, 'B', 1, 'C'),
(1, 'B', 2, 'A'),(1, 'B', 2, 'B'),(1, 'B', 2, 'C'),
(1, 'B', 3, 'A'),(1, 'B', 3, 'B'),(1, 'B', 3, 'C'),
(1, 'C', 1, 'A'),(1, 'C', 1, 'B'),(1, 'C', 1, 'C'),
(1, 'C', 2, 'A'),(1, 'C', 2, 'B'),(1, 'C', 2, 'C'),
(1, 'C', 3, 'A'),(1, 'C', 3, 'B'),(1, 'C', 3, 'C'),
(2, 'A', 1, 'A'),(2, 'A', 1, 'B'),(2, 'A', 1, 'C'),
...
"""

# ex3.2:permutations
# permutations(p[, r])
"""
全排列
创建一个迭代器,返回iterable中所有长度为r的项目序列,
如果省略了r,那么序列的长度与iterable中的项目数量相同:
返回p中任意取r个元素做排列的元组的迭代器
"""
p="abc"
for x in itertools.permutations(p):
    print(x)
"""
output:
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
"""

# ex3.3:combinations
# combinations(p, r)
"""
组合,参加ex3.2
"""
p = "abcd"
for x in itertools.combinations(p,2):   #相当于C(4,2)
    print(x)
"""
output:
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
('c', 'd')
"""


# ex3.4:combinations_with_replacement
# combinations_with_replacement(p, r)
"""
这也是组合,但是允许重负例如‘abc’中求C(3,2)
在ex3.3中有ab,ac,bc
在本立中则有:aa,ab,ac,bb,bc,cc
"""
p = "abc"
for x in itertools.combinations_with_replacement(p,2):   #相当于C(4,2)
    print(x)
"""
output:
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
"""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值