关于python的itertools模块

这是一个强大的模块

先来看一下它都有什么工具

无穷循环器

迭代器         参数         结果                                                例子

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

处理输入序列迭代器

迭代器          参数            结果                                        例子

chain()     p, q, ...           p0, p1, ... plast, q0, q1, ...              chain(‘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

groupby()   iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)

ifilterfalse()  pred, seq       elements of seq where pred(elem) is False   ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

islice()    seq, [start,] stop [, step] elements from seq[start:stop:step]  islice(‘ABCDEFG‘, 2, None) --> C D E F G

imap()      func, p, q, ...     func(p0, q0), func(p1, q1), ...             imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000

starmap()   func, seq           func(*seq[0]), func(*seq[1]), ...           starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

tee()       it, n               it1, it2 , ... itn splits one iterator into n

takewhile() pred, seq           seq[0], seq[1], until pred fails            takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

izip()      p, q, ...           (p[0], q[0]), (p[1], q[1]), ...             izip(‘ABCD‘, ‘xy‘) --> Ax By

zip_longest  iter            

组合工具

迭代器          参数                        结果

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
时间: 2025-01-21 20:59:10

关于python的itertools模块的相关文章

python 之 itertools模块

官方:https://yiyibooks.cn/xx/python_352/library/itertools.html 参考: https://blog.csdn.net/neweastsun/article/details/51965226 https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143200162233153835cfdd1a541a18ddc15059e3d

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

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

Python的itertools模块

本章将介绍Python自建模块itertools,更多内容请参考:Python参考指南 python的自建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个无限迭代器: >>>import itertools >>>natuals = itertools.count(1) >>>for n in natuals: print(n) 1 2 3 ... 因为count()会创建一个无限的迭代器,所以

Python:itertools模块

itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. chain(iter1, iter2, ..., iterN): 给出一组迭代器(iter1, iter2, ..., iterN),此函数创建一个新迭代器来将所有的迭代器链接起来,返回的迭代器从iter1开始生成项,知道iter1被用完,然后从iter2生成项,这一过程会持续到iterN中所有的项都被

python中itertools模块介绍---01

itertools模块中包含了很多函数,这些函数最终都生成一个或多个迭代器,下面对这些函数进行介绍: 为了能够使用itertools中的函数,需要将该模块导入: >>>from itertools import * count(start=0,step=1): 源代码为: def count(start=0,step=1):     n=start     while True:         yield n         n+=step 从源代码可以看出,count函数产生一个生成

python,itertools模块

itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools import unittest class TestItertools(unittest.TestCase): def test_count(self): """itertools.count()无限产生自然数""" natuals = itertool

Python:itertools模块(转)

原文:http://www.cnblogs.com/cython/articles/2169009.html itertools模块包含很多创建迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. chain(iter1, iter2, ..., iterN): 给出一组迭代器(iter1, iter2, ..., iterN),此函数创建一个新迭代器来将所有的迭代器链接起来,返回的迭代器

python 迭代器 itertools模块中常用工具函数

迭代器 itertools模块中常用工具函数,提供了接近二十个迭代器工具函数. 原文地址:https://www.cnblogs.com/bcyczhhb/p/11809842.html

python中itertools模块介绍---03

product(*iterables[,repeat]): 源代码: def product(*args,**kwds):     pools=map(tuple,args)*kwds.get("repeat",1)     result=[[]]     for pool in pools:         result=[x+[y] for x in result for y in pool]     for prod in result:         yield tuple(