Python:itertools库的使用
转于:https://blog.csdn.net/neweastsun/article/details/51965226
介绍
itertools是python内置的模块,使用简单且功能强大,这里尝试汇总整理下,并提供简单应用示例;如果还不能满足你的要求,欢迎加入补充。
使用只需简单一句导入:import itertools
chain()
与其名称意义一样,给它一个列表如 lists/tuples/iterables,链接在一起;返回iterables对象。
letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
print(list(itertools.chain(letters,booleans)))
#输出:['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1]
print(tuple(itertools.chain(letters,letters[3:])))
#输出('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f')
print(set(itertools.chain(letters,letters[3:])))
#输出:{'a', 'd', 'b', 'e', 'c', 'f'} print(list(itertools.chain(letters,letters[3:])))
#输出:['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f']
for item in list(itertools.chain(letters,booleans)):
print(item)
count()
生成无界限序列,count(start=0, step=1) ,示例从100开始,步长为2,循环10,打印对应值;必须手动break,count()会一直循环。
i = 0
for item in itertools.count(100,2):
i += 1
if i > 10 : break
print(item)
filterfalse ()
Python filterfalse(contintion,data) 迭代过滤条件为false的数据。如果条件为空,返回data中为false的项;
booleans = [1, 0, 1, 0, 0, 1]
numbers = [23, 20, 44, 32, 7, 12]
print(list(itertools.filterfalse(None,booleans)))
#输出:[0, 0, 0] print(list(itertools.filterfalse(lambda x : x < 20,numbers)))
#输出:[23, 20, 44, 32]
compress()
返回我们需要使用的元素,根据b集合中元素真值,返回a集中对应的元素。
print(list(itertools.compress(letters,booleans)))
# ['a', 'c', 'f']
starmap()
针对list中的每一项,调用函数功能。starmap(func,list[]) ;
starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
>>> from itertools import *
>>> x = starmap(max,[[5,14,5],[2,34,6],[3,5,2]])
>>> for i in x:
>>> print (i)
14
34
5
repeat()
repeat(object[, times]) 重复times次;
repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq );当函数f执行返回假时, 开始迭代序列
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable);返回序列,当predicate为true是截止。
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]);返回序列seq的从start开始到stop结束的步长为step的元素的迭代器
for i in islice("abcdef", 0, 4, 2):#a, c
print i
product()
product(iter1,iter2, ... iterN, [repeat=1]);创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,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
for i in product([1, 2, 3], [4, 5], [6, 7]):
print i
(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)
permutations()
permutations(p[,r]);返回p中任意取r个元素做排列的元组的迭代器
for i in permutations([1, 2, 3], 3):
print i
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
combinations()
combinations(iterable,r);创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序
note:不带重复
for i in combinations([1, 2, 3], 2):
print i
(1, 2)
(1, 3)
(2, 3)
combinations_with_replacement()
同上, 带重复 例子:
for i in combinations_with_replacement([1, 2, 3], 2):
print i
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
应用示例
求质数序列中1,3,5,7,9,11,13,15三个数之和为35的三个数;
def get_three_data(data_list,amount):
for data in list(itertools.combinations(data_list, 3)):
if sum(data) == amount:
print(data)
#(7, 13, 15)
#(9, 11, 15)
Python:itertools库的使用的更多相关文章
- [python] itertools库学习
最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...
- Python标准库笔记(10) — itertools模块
itertools 用于更高效地创建迭代器的函数工具. itertools 提供的功能受Clojure,Haskell,APL和SML等函数式编程语言的类似功能的启发.它们的目的是快速有效地使用内存, ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- 转--Python标准库之一句话概括
作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...
- Python标准库笔记(11) — Operator模块
Operator--标准功能性操作符接口. 代码中使用迭代器时,有时必须要为一个简单表达式创建函数.有些情况这些函数可以用一个lambda函数实现,但是对于某些操作,根本没必要去写一个新的函数.因此o ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- python 标准库大全
python 标准库 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata:Unicode字符数据库 string ...
- 这段代码很Pythonic | 相见恨晚的 itertools 库
前言 最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了. 很多人都致力于把Python代码写得更 ...
- python 常用库收集
读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...
- python+paramiko库+svn写的自动化部署脚本
第一篇博文 直接开门见山的说了. 这是件什么事?:每次部署都是复制本地的文件粘贴到服务器端,因为路径复杂,所以费时且手工容易出漏洞. 一直在想有什么办法可以解决这种,因为以前在微软的一个牛人同事做过一 ...
随机推荐
- javascript中apply和call的区别
请补充 136页 pdf 高级javascript设计
- Webpack探索【2】--- 安装、项目初始化、webpack.config.js配置文件
本文主要讲安装.项目初始化.webpack.config.js配置文件方面的内容.
- Android系统移植与调试之------->如何修改Android默认字体大小和设置里面字体大小比例
因为我修改 ro.sf.lcd_density的值,将它从160修改 为120,所以导致整个系统的字体都变得很小.因此需要将整个字体变大,并且在设置-->显示-->字体大小的4个选项的值都 ...
- pinpoint插件开发实践
plugin基本结构 一个plugin主要由三部分构成,插件类增强定义(ProfilerPlugin接口实现).插件描述定义(TraceMetadataProvider接口实现).增强类拦截器实现(A ...
- CentOS7 添加用户到 sudoers
1.在装完系统后,CentOS默认普通用户是无法使用sudo命令的,这时我们就需要将登陆的用户加入/etc/sudoers 文件中 假设用户名是yhd 3.切换到超级用户:$ su - 当然输入的 p ...
- Kattis - horrorfilmnight 【贪心】
题意 有两个人想去一起看电影,然后分别给出两个人 分别喜欢看的电影都在哪些天 然后 同一个人 不能连续看两天他不喜欢的电影 求他们最多可以看多少次电影 思路 先将两人喜欢看的电影进行排序, ① 选择两 ...
- cookie补充
之前写cookie中关于突破同源策略共享cookie说的比较含糊,此次来详细说明一下: ## 首先说一下cookie的path和domain这 两个属性值 path: path表示的此条cookie是 ...
- dede调用二级下拉菜单方法
<div id="menu"> <ul> {dede:channelartlist typeid='top' row='6' orderby='s ...
- apache下配置多域名多目录的应用
引言:阿里云centos apache web服务器中配置不同域名访问不同的目录,达到类似增加虚拟主机的效果: 案例: 如有2个www.a.com ,www.b.com 域名, 访问www.a.com ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...