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), repeat(object, times)(将object循环n次,不是循环object里面的元素,就是循环它本身,不一定要是可迭代对象),

freemao

FAFU

[email protected]

python, itertools模块

时间: 2024-08-04 02:55:14

python, itertools模块的相关文章

python itertools模块练习

参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 迭代器 1.chain() 处理多个序列,而不比构造一个大的,两个合在一起,遍历就好了 >>> for i in chain(range(3),range(5,9)): ... print i ... 0 1 2 5 6 7 8 >>> 2.izip() 类似zip,可以

python itertools 模块讲解

1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存.使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环.- 无限迭代器 itertools包自带了三个可以无限迭代的迭代器.这意味着,当你使用他们时,你要知道要的到底是最终会停止的迭代器,还是需要无限地迭代鞋去.(1)count(初值=0, 步长=1):count 迭代器会返回从传入的起始参数开始的均匀间隔的数值.count 也可以接收指定的步长参数.我们来看一个简单的例子: ''' 遇到

Python itertools模块详解

这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com/2/itertools/ 基本是基于文档的翻译和补充,相当于翻译了 itertools用于高效循环的迭代函数集合 组成 总体,整体了解 无限迭代器 复制代码代码如下: 迭代器 参数 结果 例子 count() start, [step] start, start+step, start+2*step, ...

python itertools模块实现排列组合

转自:https://blog.csdn.net/specter11235/article/details/71189486 一.笛卡尔积:itertools.product(*iterables[, repeat]) 直接对自身进行笛卡尔积: import itertools for i in itertools.product('ABCD', repeat = 2): print (''.join(i),end=' ') 输出结果: AA AB AC AD BA BB BC BD CA CB

python - itertools 模块

相关文档 文档 链接 pymotw 链接 无限迭代器 itertools.count() 说明 生成一个无限迭代的数字队列, 只有进行 参数 其实数字以及步幅 start, [step] 返回值 start, start+step, start+2*step, ... 示例 count(10) --> 10 11 12 13 14 ... from itertools import * import time c = count(10,5) print c for i in c: time.sl

python高级编程之生成器表达式和itertools模块

# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #生成器表达式和itertools模块 #yield 中可以使用圆括号代替中括号 iter0=(x**2 for x  in range(10)if x%2==0) for iter1 in iter0: print iter1 #结果 """ 0 4 16 36 64 """ #这样的表达式被称为生成器或者gene

Python学习笔记—itertools模块

这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较费劲... 1.itertools.count(start=0,step=1) 此函数用来创建一个迭代器,生成从n开始的连续整数,如果忽略n,则从0开始计算 如果超出了sys.maxint,计数器将溢出并继续行-sys.maxint-1开始计算 定义: def count(start=0, step=1):

python 高效的 itertools 模块

迭代器的特点是:惰性求值(Lazy evaluation),即只有当迭代至某个值时,它才会被计算,这个特点使得迭代器特别适合于遍历大文件或无限集合等,因为我们不用一次性将它们存储在内存中. Python 内置的 itertools 模块包含了一系列用来产生不同类型迭代器的函数或类,这些函数的返回都是一个迭代器,我们可以通过 for 循环来遍历取值,也可以使用 next() 来取值. itertools 模块提供的迭代器函数有以下几种类型: 无限迭代器:生成一个无限序列,比如自然数序列 1, 2,

python中itertools模块zip_longest函数实现逻辑

最近在看流畅的python,在看第14章节的itertools模块,对其itertools中的相关函数实现的逻辑的实现 其中在zip_longest(it_obj1, ..., it_objN, fillvalue=None)时,其函数实现的功能和内置zip函数大致相同(实现一一对应), 不过内置的zip函数是已元素最少对象为基准,而zip_longest函数是已元素最多对象为基准,使用fillvalue的值来填充 以下是自己总结此函数的大致实现方法,和官方方法不同: 思路大致如此: 找出元素个