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解释器自身提供的小工具.这些小工具在笔者的日常工作中经常用到,减少了各种时间的浪费,然而,却很容易被大家忽略.每当有新来的同事看到我这么使用时,都忍不住感叹, ...
随机推荐
- @autowired注解报错原因及解决办法
@autowired 注入dao层的时候,标红报错,但不影响编译使用 按照严格的spring注解方式在dao层加入@Repository注解
- 【论文随笔】基于会话的推荐系统构建方法调查(Survey On Methods For Building Session-Based Recommender Systems)
前言 今天读的论文为一篇于2023年发表在国际开放信息技术杂志(International Journal of Open Information Technologies)的论文,文章是关于构建基于 ...
- ABAQUS 中的一些约定
目录 自由度notation Axisymmetric elements Activation of degrees of freedom Internal variables in Abaqus/S ...
- 【Matlab】求解复合材料层合板刚度矩阵及柔度矩阵
1. matlab文件结构 2. main.m代码 clc clear; warning off; %% %铺层角度数组 angles=[0 90 0]; % ° %单层厚度 ply_thicknes ...
- CORS 跨域请求一种解决方案
平常工作难遇到这类问题, 一般搭建新系统或搭建系统二时需要复用系统一一些前后端能力, 可能会遇到跨域拦截问题. 这里提供一种基于服务器解决方案. 更多其他方案, 详细细节可自行查阅更多资料, 写一些前 ...
- go json omitempty 关键字 脱坑
用法 大家对于 json 和 struct 之间的转换一定不陌生,为了将代码中的结构体与 json 数据解耦,通常我们会在结构体的 field 类型后加上解释说明,例如在表示一个地址的时候, json ...
- Golang 入门 : 变量
变量 Go语言是静态强类型语言,所以变量是有明确类型的.变量实质上就是在内存中的一小块空间,用来存储特定类型的可变数据.如果没有变量我们的程序只能将数值写死都是静态的数据,无法更改,变量可以让我们进行 ...
- js 时间转时间戳
前言 有时候我们用时间插件,选择好时间后,需要把日期格式转化为时间戳,再传到后台 时间转时间戳 let time = Math.floor(new Date("2014-04-23 18:5 ...
- 从 PostgreSQL 升级至 IvorySQL 4.0
本文作者:严少安,IvorySQL 贡献者. 本文为授权转载. 2024 年 8 月,我在<PG 12 即将退役,建议升级到 16.4>一文中提到,PostgreSQL 12 版本即将&q ...
- HashMap遍历方法
HashMap是Java中非常常用的集合类,用于存储键值对映射.遍历HashMap的方法有多种,每种方法有其特定的用途和效率.以下是几种常用的遍历方法: 1. 使用 entrySet 遍历 这是最常用 ...