python itertools的使用(转)
1. chain的使用
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
for item in itertools.chain(listone,listtwo):
print item
输出: a b c 11 22 abc
2. count的使用
i = 0
for item in itertools.count(100):
if i>10:
break
print item,
i = i+1
功能:从100开始数10个数,cout返回一个无界的迭代器,如果引入一个计数I,可以让它计数10次。。
输出:100 101 102 103 104 105 106 107 108 109 110
3.cycle的使用
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
for item in itertools.cycle(listone):
print item,
功能:从列表中取元素,到列表尾后再从头取...
无限循环,因为cycle生成的是一个无界的失代器
输出:a b c a b c a b c a b c a b c a b c a b c a b c a b c...
4.ifilter的使用,ifilter(fun,iterator)返回一个可以让fun返回True的迭代器
import itertools
def funLargeFive(x):
if x > 5:
return True
for item in itertools.ifilter(funLargeFive,range(-10,10)):
print item,
结果:6 7 8 9
5. imap的使用,imap(fun,iterator)返回一个迭代器,对iterator中的每个项目调用fun
import itertools
listthree = [1,2,3]
def funAddFive(x):
return x + 5
for item in itertools.imap(funAddFive,listthree):
print item,
返回:6 7 8 对listthree中的元素每个加了5后返回给迭代器
6.islice的使用,islice()(seq, [start,] stop [, step])
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
listthree = listone + listtwo
for item in itertools.islice(listthree,3,5):
print item,
功能:返回迭代器,其中的项目来自 将seq,从start开始,到stop结束,以step步长切割后
打印出:11 22
7.izip的使用,izip(*iterator)
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
listthree = listone + listtwo
for item in itertools.izip(listone,listtwo):
print item,
结果:('a', '11') ('b', '22') ('c', 'abc')
功能:返回迭代器,项目是元组,元组来自*iterator的组合
8. repeate,repeate(elem [,n])
import itertools
listone = ['a','b','c']
for item in itertools.repeat(listone,3):
print item,
结果:['a', 'b', 'c'] ['a', 'b', 'c'] ['a', 'b', 'c']
python itertools的使用(转)的更多相关文章
- 转:Python itertools模块
itertools Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个"无限"迭代器: >>& ...
- python, itertools模块
通过itertools模块,可以用各种方式对数据进行循环操作 1, chain() from intertools import chain for i in chain([1,2,3], ('a', ...
- python itertools 模块
Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...
- 《笔记》Python itertools的groupby分组数据处理
今天遇到这么一个需求,需要将这样的数据进行分组处理: [(, ), (, ), (, ), (, ), (, ), (, )] 处理之后我可能需要得到这样的结果: [(, (, , (, , (, ) ...
- Python itertools模块详解
这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com/2/iter ...
- python itertools 模块讲解
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...
- Python itertools/内置函数
https://docs.python.org/3.5/library/itertools.html#itertools.starmap // https://docs.python.org/3.5/ ...
- Python itertools.combinations 和 itertools.permutations 等价代码实现
最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- [python] itertools库学习
最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...
随机推荐
- Android蓝牙通信总结
这篇文章要达到的目标: 1.介绍在Android系统上实现蓝牙通信的过程中涉及到的概念. 2.在android系统上实现蓝牙通信的步骤. 3.在代码实现上的考虑. 4.例子代码实现(手持设备和蓝牙串口 ...
- 【BZOJ4517】【SDOI2016】排列计数 [数论]
排列计数 Time Limit: 60 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 求有多少种长度为 n 的序列 A, ...
- Quick-Cocos2dx-Community_3.6.3_Release 编译时libtiff.lib 无法解析
Quick-Cocos2dx-Community_3.6.3_Release 使用VS2012编译,报错: libtiff.lib lnk2001 无法解析的外部符号 ltod3 类似于上面这种,刚才 ...
- python大数据挖掘系列之淘宝商城数据预处理实战
数据清洗: 所谓的数据清洗,就是把一些异常的.缺失的数据处理掉,处理掉不一定是说删除,而是说通过某些方法将这个值补充上去,数据清洗目的在于为了让我们数据的可靠,因为脏数据会对数据分析产生影响.拿到数据 ...
- 安装Vue.js devtools
1.下载安装 https://github.com/vuejs/vue-devtools#vue-devtools 通过以上地址下载安装包,解压以后进入文件,按住shift,点击鼠标右键打开命令窗口 ...
- python3中处理url异常
import urllib.request import urllib.error url = 'http://c.telunyun.com/Chart/getJsonData?market=1' d ...
- exit() _exit() 函数区别
exit(): --stdlib.h (1) 所有使用atexit()注册的函数,将会被以注册相反的顺序调用: (2) 所有打开的输出流被刷新,并且关闭流: (3) 使用tmpfile()创建的文件将 ...
- 基于 Arduino 开发板,这款插座是可编程且开源的
基于 Arduino 开发板,这款插座是可编程且开源的 https://www.oschina.net/news/74861/open-source-socket https://github.com ...
- Win7蓝屏代码0X0000007B可能是SATA mode问题
Win7蓝屏代码0X0000007B可能是硬盘模式的问题,我进入BIOS把SATA的mode从Enhanced改为Compatible(及IDE兼容模式)结果系统可以顺利启动没有问题. 从 ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...