Python内置库itertools简单学习
该库为满足特定需要的比较高效的迭代器内置库,在数据科学中的应用也不少,故有必要了解一下:
import itertools
import sys
无限迭代器(Infinite iterators)
| Iterator | Arguments | Results | Example |
|---|---|---|---|
count() |
start, [step] | start, start+step, start+2*step, … | count(10) --> 10 11 12 13 14 ... |
cycle() |
p | p0, p1, … plast, p0, p1, … | cycle('ABCD') --> A B C D A B C D ... |
repeat() |
elem [,n] | elem, elem, elem, … endlessly or up to n times | repeat(10, 3) --> 10 10 10 |
- itertools.count
a=itertools.count(5)
type(a)
itertools.count
next(a)
5
next(a)
6
next(a)
7
b=itertools.count(6,2)
next(b)
6
next(b)
8
- itertools.cycle
a=itertools.cycle([1,2])
next(a)
1
next(a)
2
next(a)
1
next(a)
2
b=itertools.cycle('ABC')
next(b)
'A'
next(b)
'B'
next(b)
'C'
next(b)
'A'
- itertools.repeat
a=itertools.repeat([1,2,3])
next(a)
[1, 2, 3]
next(a)
[1, 2, 3]
b=itertools.repeat('ABC',3)
next(b)
'ABC'
next(b)
'ABC'
next(b)
'ABC'
try:
next(b)
except Exception as e:
print(sys.exc_info())
(<class 'StopIteration'>, StopIteration(), <traceback object at 0x0000024CB3EE2E08>)
最短输入停止迭代器(Iterators terminating on the shortest input sequence)
| Iterator | Arguments | Results | Example |
|---|---|---|---|
accumulate() |
p [,func] | p0, p0+p1, p0+p1+p2, … | accumulate([1,2,3,4,5]) --> 1 3 6 10 15 |
chain() |
p, q, … | p0, p1, … plast, q0, q1, … | chain('ABC', 'DEF') --> A B C D E F |
chain.from_iterable() |
iterable | p0, p1, … plast, q0, q1, … | chain.from_iterable(['ABC', 'DEF']) --> A B C D E F |
compress() |
data, selectors | (d[0] if s[0]), (d[1] if s[1]), … | compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F |
dropwhile() |
pred, seq | seq[n], seq[n+1], starting when pred fails | dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 |
filterfalse() |
pred, seq | elements of seq where pred(elem) is false | filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 |
groupby() |
iterable[, key] | sub-iterators grouped by value of key(v) | |
islice() |
seq, [start,] stop [, step] | elements from seq[start:stop:step] | islice('ABCDEFG', 2, None) --> C D E F G |
starmap() |
func, seq | func(seq[0]), func(seq[1]), … | starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 |
takewhile() |
pred, seq | seq[0], seq[1], until pred fails | takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 |
tee() |
it, n | it1, it2, … itn splits one iterator into n | |
zip_longest() |
p, q, … | (p[0], q[0]), (p[1], q[1]), … | zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
- itertools.accumulate
a=itertools.accumulate([1,2,3,4])
list(a)
[1, 3, 6, 10]
b=itertools.accumulate('abc')
list(b)
['a', 'ab', 'abc']
- itertools.chain
a=itertools.chain([1,2],[3,4],[5,6])
list(a)
[1, 2, 3, 4, 5, 6]
b=itertools.chain('ab','cd')
list(b)
['a', 'b', 'c', 'd']
- itertools.chain.from_iterable
a=itertools.chain.from_iterable(['abc','def'])
list(a)
['a', 'b', 'c', 'd', 'e', 'f']
- itertools.compress
a=itertools.compress('abcdef',[1,0,1,0,1,1])
list(a)
['a', 'c', 'e', 'f']
a=itertools.compress('abcdef',[1,False,1,False,1,0])
list(a)
['a', 'c', 'e']
- itertools.dropwhile
a=itertools.dropwhile(lambda x:x<5,[1,4,6,4,1])
list(a)
[6, 4, 1]
- itertools.takewhile
a=itertools.takewhile(lambda x:x<5,[1,4,6,4,1])
list(a)
[1, 4]
- filterfalse
a=itertools.filterfalse(lambda x:x%2,range(10))
list(a)
[0, 2, 4, 6, 8]
- groupby
x = [(1, 2), (2, 3), (1, 4), (5, 5), (3, 4), (2, 6)]
a=itertools.groupby(x,lambda x:x[0])
for i in a:
print(i[0],list(i[1]))
1 [(1, 2)]
2 [(2, 3)]
1 [(1, 4)]
5 [(5, 5)]
3 [(3, 4)]
2 [(2, 6)]
- itertools.islice
a=itertools.islice('abcdefg',2,None)
list(a)
['c', 'd', 'e', 'f', 'g']
b=itertools.islice('abcdefg',2,4)
list(b)
['c', 'd']
- itertools.starmap
a=itertools.starmap(pow,[(2,5),(3,2)])
list(a)
[32, 9]
- itertools.tee
a=itertools.tee([1,2,3],2)
list(a)
[<itertools._tee at 0x24cb3f29448>, <itertools._tee at 0x24cb3f29888>]
a=itertools.tee([1,2,3],2)
list(a[0])
[1, 2, 3]
list(a[1])
[1, 2, 3]
- itertools.zip_longest
a=itertools.zip_longest('abcd','xy',fillvalue='-')
list(a)
[('a', 'x'), ('b', 'y'), ('c', '-'), ('d', '-')]
组合迭代器
| Iterator | Arguments | Results |
|---|---|---|
product() |
p, q, … [repeat=1] | cartesian product, equivalent to a nested for-loop |
permutations() |
p[, r] | r-length tuples, all possible orderings, no repeated elements |
combinations() |
p, r | r-length tuples, in sorted order, no repeated elements |
combinations_with_replacement() |
p, r | r-length tuples, in sorted order, with repeated elements |
product('ABCD', repeat=2) |
AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD |
|
permutations('ABCD', 2) |
AB AC AD BA BC BD CA CB CD DA DB DC |
|
combinations('ABCD', 2) |
AB AC AD BC BD CD |
|
combinations_with_replacement('ABCD', 2) |
AA AB AC AD BB BC BD CC CD DD |
- itertools.product
a=itertools.product([1,2,3],[4,5,6])
list(a)
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
a=itertools.product([1,2,3],[4,5,6],repeat=2)
list(a)
[(1, 4, 1, 4),
(1, 4, 1, 5),
(1, 4, 1, 6),
(1, 4, 2, 4),
(1, 4, 2, 5),
(1, 4, 2, 6),
(1, 4, 3, 4),
(1, 4, 3, 5),
(1, 4, 3, 6),
(1, 5, 1, 4),
(1, 5, 1, 5),
(1, 5, 1, 6),
(1, 5, 2, 4),
(1, 5, 2, 5),
(1, 5, 2, 6),
(1, 5, 3, 4),
(1, 5, 3, 5),
(1, 5, 3, 6),
(1, 6, 1, 4),
(1, 6, 1, 5),
(1, 6, 1, 6),
(1, 6, 2, 4),
(1, 6, 2, 5),
(1, 6, 2, 6),
(1, 6, 3, 4),
(1, 6, 3, 5),
(1, 6, 3, 6),
(2, 4, 1, 4),
(2, 4, 1, 5),
(2, 4, 1, 6),
(2, 4, 2, 4),
(2, 4, 2, 5),
(2, 4, 2, 6),
(2, 4, 3, 4),
(2, 4, 3, 5),
(2, 4, 3, 6),
(2, 5, 1, 4),
(2, 5, 1, 5),
(2, 5, 1, 6),
(2, 5, 2, 4),
(2, 5, 2, 5),
(2, 5, 2, 6),
(2, 5, 3, 4),
(2, 5, 3, 5),
(2, 5, 3, 6),
(2, 6, 1, 4),
(2, 6, 1, 5),
(2, 6, 1, 6),
(2, 6, 2, 4),
(2, 6, 2, 5),
(2, 6, 2, 6),
(2, 6, 3, 4),
(2, 6, 3, 5),
(2, 6, 3, 6),
(3, 4, 1, 4),
(3, 4, 1, 5),
(3, 4, 1, 6),
(3, 4, 2, 4),
(3, 4, 2, 5),
(3, 4, 2, 6),
(3, 4, 3, 4),
(3, 4, 3, 5),
(3, 4, 3, 6),
(3, 5, 1, 4),
(3, 5, 1, 5),
(3, 5, 1, 6),
(3, 5, 2, 4),
(3, 5, 2, 5),
(3, 5, 2, 6),
(3, 5, 3, 4),
(3, 5, 3, 5),
(3, 5, 3, 6),
(3, 6, 1, 4),
(3, 6, 1, 5),
(3, 6, 1, 6),
(3, 6, 2, 4),
(3, 6, 2, 5),
(3, 6, 2, 6),
(3, 6, 3, 4),
(3, 6, 3, 5),
(3, 6, 3, 6)]
- itertools.permutations
for i in itertools.permutations([1,2,3],2):
print (i)
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
for i in itertools.permutations('123',2):
print (i)
('1', '2')
('1', '3')
('2', '1')
('2', '3')
('3', '1')
('3', '2')
for i in itertools.permutations('122',2):
print (i)
('1', '2')
('1', '2')
('2', '1')
('2', '2')
('2', '1')
('2', '2')
- itertools.combinations
a=itertools.combinations([1,2,3],2)
list(a)
[(1, 2), (1, 3), (2, 3)]
a=itertools.combinations('abcd',2)
list(a)
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
- itertools.combinations_with_replacement
a=itertools.combinations_with_replacement('abcd',2)
list(a)
[('a', 'a'),
('a', 'b'),
('a', 'c'),
('a', 'd'),
('b', 'b'),
('b', 'c'),
('b', 'd'),
('c', 'c'),
('c', 'd'),
('d', 'd')]
Python内置库itertools简单学习的更多相关文章
- python内置函数的简单使用和介绍
"""内置函数的简单使用和介绍参考链接:https://docs.python.org/3/library/functions.html ""&quo ...
- Python 内置库 sys用法
sys模块功能众多,这边先学习几个常用的方法sys常见函数列表① sys.argv: 实现从程序外部向程序传递参数.其实sys.argv[]就是一个列表,里面的项为用户输入的参数,但是sys.argv ...
- 使用python内置库pytesseract实现图片验证码的识别
环境准备: 1.安装Tesseract模块 git文档地址:https://digi.bib.uni-mannheim.de/tesseract/ 下载后就是一个exe安装包,直接右击安装即可,安装完 ...
- 使用python内置库matplotlib,实现折线图的绘制
环境准备: 需要安装matplotlib,安装方式: pip install matplotlib 直接贴代码喽: #引入模块 from matplotlib import pyplot,font_m ...
- Python学习:6.python内置函数
Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...
- python算法常用技巧与内置库
python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...
- 常用的python的内置库或者第三方库
内置库:re,json,time,random,sys,os, 第三方库:转载: https://www.cnblogs.com/jiangchunsheng/p/9275881.htmlReques ...
- python内置函数简单归纳
做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...
- RF内置库-----内置库的学习过程总结
前段时间充忙的学习RF,系统学习完之后就开始动手做各种接口的测试,虽然各类的接口测试基本能跑通了,但是重复造车的问题存在太明显.RF本身内置库就已经比较丰富,比如不需要import直接就加载到内存的B ...
- 几个可以提高工作效率的Python内置小工具
在这篇文章里,我们将会介绍4个Python解释器自身提供的小工具.这些小工具在笔者的日常工作中经常用到,减少了各种时间的浪费,然而,却很容易被大家忽略.每当有新来的同事看到我这么使用时,都忍不住感叹, ...
随机推荐
- 附039.Kubernetes_v1.32.2高可用部署架构二
部署组件 该 Kubernetes 部署过程中,对于部署环节,涉及多个组件,主要有 kubeadm .kubelet .kubectl. kubeadm介绍 Kubeadm 为构建 Kubernete ...
- RedHat8密码复杂度策略配置
1.密码复杂度策略概念 在Linux系统中,确保用户密码的复杂度是提高系统安全性的重要措施之一.通过配置密码策略,可以强制用户使用强密码,从而降低被破解的风险.本文将详细介绍如何在 Linux 系统中 ...
- 读论文-序列感知推荐系统(Sequence-Aware Recommender Systems)
前言 今天读的论文为一篇于2018年发表在(ACM computing surveys (CSUR))的论文,这篇文章主要讲述了序列感知推荐系统(Sequence-Aware Recommender ...
- 牛逼了!16.2K Star!推荐一款开源的网络爬虫和浏览器自动化库:Crawlee!
在当今的互联网世界中,网络爬虫作为一种重要的工具,被广泛应用于数据收集.内容监控.SEO优化以及自动化测试等多个领域.随着技术的不断进步,各种开源的网络爬虫库也应运而生.今天,我向大家推荐一款非常优秀 ...
- Proteus中数码管动态扫描显示不全(已解决)
前言 我是直接把以前写的 51 数码管程序复制过来的,当时看的郭天祥的视频,先送段选,消隐后送位选,最后来个 1ms 的延时. 代码在 Proteus 中数码管静态是可以的,动态显示出了问题--显示不 ...
- 归并排序(递归)(NB)
博客地址:https://www.cnblogs.com/zylyehuo/ 递归思路 # _*_coding:utf-8_*_ import random def merge(li, low, mi ...
- nginx 部署配置
下载nginx 包(http://nginx.org/en/download.html) nginx常用命令: nginx -s stop 快速关闭Nginx,可能不保存相关信息,并终止w ...
- DeepSeek引发的AI发展路径思考
DeepSeek引发的AI发展路径思考 参考文章来源于科技导报 ,作者李国杰院士 | 哈工大 DeepSeek 技术前沿与应用讲座 1. DeepSeek 的科技突破 7 天之内 DeepSeek 的 ...
- Python 生成器说明
生成器 python 生成器 常规形态 # list def square_numbers(nums: list): squared_nums = [] for i in nums: squared_ ...
- 基于RK3568 + FPGA国产平台的多通道AD实时采集显示方案分享
在工业控制与数据采集领域,高精度的AD采集和实时显示至关重要.今天,我们就来基于瑞芯微RK3568J + FPGA国产平台深入探讨以下,它是如何实现该功能的.适用开发环境如下: Windows开发环境 ...