python itertools_Python之itertools库

自己在做深度学习项目时,看到很多的大佬使用这个库,后来感觉真的使用这个库自己的代码量会减少很多,所以整理一下啦~~LeetCode这两天会接着开更,前几天忙着项目和考试了,哈哈,太烦了~~有很多需要整理的东西~

使用方法:import itertools

官方文档:9.7. itertools - Functions creating iterators for efficient looping - Python 2.7.15 documentation​docs.python.org

一、无限迭代器--Infinite Iterators

itertools.count(start=0,step=1)# count(10) --> 10 11 12 13 14 ...

# count(2.5, 0.5) -> 2.5 3.0 3.5 ...

itertools.cycle(iterable)# cycle('ABCD') --> A B C D A B C D A B C D ...

itertools.repeat(object[, times])# repeat(10, 3) --> 10 10 10

二、处理输入序列迭代器

itertools.chain(*iterables) : 从多个迭代器转化为一个# chain('ABC', 'DEF') --> A B C D E F

itertools.compress(data, selectors) : 筛选输入序列# compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F

itertools.groupby(iterable[, key]) : 返回一个产生按照key进行分组后的值集合的迭代器.# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B

# [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D

itertools.ifilter(predicate, iterable) :当测试函数为True的时候返回该值,与dropwhile不同# ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9

itertools.ifilterfalse(predicate, iterable)# ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

itertools.islice(iterable, start, stop[, step])# islice('ABCDEFG', 2) --> A B

# islice('ABCDEFG', 2, 4) --> C D

# islice('ABCDEFG', 2, None) --> C D E F G

# islice('ABCDEFG', 0, None, 2) --> A C E G

itertools.imap(function, *iterables) : 类似于map函数# imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000

itertools.starmap(function, iterable)# starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

itertools.takewhile(predicate, iterable) :与ifilter不同,这函数从序列头开始执行,只要遇到predicate计算为false, 那么迭代就停止# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

itertools.dropwhile(predicate, iterable) :与ifilterfalse不同,这函数从序列头开始执行,只要遇到predicate计算为false, 那么迭代就停止, 与上面的takewhile互为相反数# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

itertools.izip(*iterables) : 返回一个合并了多个迭代器为一个元组的迭代器. 它类似于内置函数zip(), 只是它返回的是一个迭代器而不是一个列表# izip('ABCD', 'xy') --> Ax By

itertools.izip_longest(*iterables[, fillvalue])# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-

三、组合生成器

itertools.product(*iterables[, repeat]) :repeat表示这种乘积的重复次数# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy

# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

itertools.permutations(iterable[, r]) :进行排列# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC

# permutations(range(3)) --> 012 021 102 120 201 210

itertools.combinations(iterable, r) :进行排列,但是最后生成的没有重复的# combinations('ABCD', 2) --> AB AC AD BC BD CD

# combinations(range(4), 3) --> 012 013 023 123

itertools.combinations_with_replacement(iterable, r):带重复,含有本身# combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值