python, itertools模块
通过itertools模块,可以用各种方式对数据进行循环操作
1, chain()
from intertools import chain
for i in chain([1,2,3], ('a', 'b', 'c'), 'abcde'):
print i
chain将可迭代的对象链接在一起,iter1循环完后,接着循环iter2.直到所有的iter循环完。
2, combinations()
from intertools import combinations
for i in combinations([1,2,3,4], 2):
print i
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
第一个参数是一个可迭代对象, 第二个参数是返回的长度。
3, dropwhile(predicate, iterable)
和fileter()的功能差不多
from intertools import dropwhile
x = lambda x : x < 5
for i in dropwhile(x, [1,2,3,4,5,6,7,8]):
print x
将iterable中不满足x的都扔掉了,只循环满足条件x的.
4, imap()
和Python 自带的map一样
eg:
from itertools import imap
x = lambda x, y : x + y
for i in imap(x, '1234', 'abcd'):
print i
1a
2b
3c
4d
x的两个参数分别来自于后面两个可迭代对象
for i in map(x, '1234', 'abcd'):
pirnt i
和上面结果一样
5,islice(iterable, start, end, step)
from itertools import islice
t = 'abcdefghijk'
for i in islice(t, 0, 5, 1):
print i
指循环s前5个元素。t[0], t[1], t[2], t[3], t[4]
for i in islice(t, 5):
print i
t之后只有一个参数,这个代表end. 如果想让这个代表start, 必须这样写: for i in islice(t, 5, None):表示从t[5]开始循环,一直到结束。步进默认为1.
6, repeat(object, times) 将object循环n次,不是循环object里面的元素,是循环它本身,不一定要是可迭代对象
from itertools import repeat
for i in repeat(5, 3):
print i
得到 5 5 5 (5是一个整数,所以不限于可迭代的对象,只是循环object本身,是什么就循环几次什么)
其他用法:izip(ite1,ite2) (等同于Python自带的zip),
freemao
FAFU
python, itertools模块的更多相关文章
- 转:Python itertools模块
itertools Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个"无限"迭代器: >>& ...
- python itertools 模块
Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...
- python itertools 模块讲解
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- Python itertools模块详解
这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com/2/iter ...
- [转载] Python itertools模块详解
原文在这里,写的很详细,感谢原作者,以下摘录要点. itertools用于高效循环的迭代函数集合. 无限迭代器 迭代器 参数 结果 例子 count() start, [step] start, st ...
- Python itertools模块中的product函数
product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...
- python itertools模块实现排列组合
转自:https://blog.csdn.net/specter11235/article/details/71189486 一.笛卡尔积:itertools.product(*iterables[, ...
- Python标准模块--itertools
1 模块简介 Python提供了itertools模块,可以创建属于自己的迭代器.itertools提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...
随机推荐
- qml package 的使用
什么时候使用这个.就是多个view使用同一个deleagte的时候. The Package class is used in conjunction with VisualDataModel to ...
- android webview web里面的数据透传到java以及java的数据透传到web
详见: http://tutorials.jenkov.com/android/android-web-apps-using-android-webview.html#android-web-app- ...
- Understanding Virtual Memory
Understanding Virtual Memory by Norm Murray and Neil Horman Introduction Definitions The Life of a P ...
- Linux配置防火墙 开启80端口
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...
- Deep Learning In NLP 神经网络与词向量
0. 词向量是什么 自然语言理解的问题要转化为机器学习的问题,第一步肯定是要找一种方法把这些符号数学化. NLP 中最直观,也是到目前为止最常用的词表示方法是 One-hot Representati ...
- 继承自NSObject的类不能用CGRect
我用的是Xcode6.2. 系统默认没有pch文件. 所以没有自动导入UIKit包. 我在继承NSObject类里也不能用CGRect或者UI开头的控件,原因也是Xcode6.2以后版本 缺少UIKi ...
- [C/C++]C/C++相关网站
1.http://en.cppreference.com What is the purpose of this site? Our goal is to provide programmers wi ...
- LepideMigrator for Documents Step by Step
blog: http://blog.csdn.net/foxdave A Manager Marketing Operations invite me to review their product, ...
- 《用格式化(fprintf和fscanf函数)的方式读写文件》
//用格式化(fprintf和fscanf函数)的方式读写文件 [用格式化的方式向文件中写入数据]#include<stdio.h>#include<stdlib.h> int ...
- [UIImage resizableImageWithCapInsets:]使用注意
转自:http://www.cnblogs.com/scorpiozj/p/3302270.html 最近在sae上搭建了个wp,因为深感自己前端的东西缺乏,所以想依次为契机,学习一下.本文是从个人的 ...