zoukankan      html  css  js  c++  java
  • python3itertools模块和迭代器函数

    补充:zip迭代器和zip_longest迭代器

    python3中的zip是可迭代的对象,可以节省内存空间

    zip(*iterables) #构造函数

    zip拼接多个可迭代对象iter1,iter2...的元素,返回新的可迭代对象,其元素为各系列iter1,iter2...对象元素组成的元组。如果各系列iter1,iter2..长度不一致,则截断至最小系列长度。

    >>> zip
    <class 'zip'>
    >>> zip((1,2,3),"abc",range(3))
    <zip object at 0x0000023E8CB3AC88>
    >>> list(zip((1,2,3),"abc",range(3)))
    [(1, 'a', 0), (2, 'b', 1), (3, 'c', 2)]
    >>> list(zip("abc",range(10)))
    [('a', 0), ('b', 1), ('c', 2)]

    多个可迭代对象的元素个数不一致时,如果需要去最大的长度,则需要使用itertools.zip_longest迭代器:zip_longest(*iterables,filvalue=None)  其中吧,fillvalue是填充值,默认为None

    >>> import itertools
    >>> list(itertools.zip_longest("ABCD","xy",fillvalue="+"))
    [('A', 'x'), ('B', 'y'), ('C', '+'), ('D', '+')]

    一.无穷序列迭代器

    itertools模块包含3个无穷序列的迭代器

    1. count(start=0,step=1) #从start开始,步长为step的无穷序列,start, start+step, start+2*step, ...,count(10) --> 10 11 12 13 14 ...
    2. cycle(iterable)               #可迭代对象iterable“元素”的无限重复,p0, p1, ... plast, p0, p1, ...,cycle('ABCD') --> ...
    3. repeat(object[,time])       #重复对象object无数次(若指定times,则重复times次),repeat(10, 3) --> 10 10 10
    >>> from itertools import *
    >>> list(zip(count(1),"abcde"))
    [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
    >>> list(zip(range(10),cycle("abc")))
    [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'a'), (4, 'b'), (5, 'c'), (6, 'a'), (7, 'b'), (8, 'c'), (9, 'a')]
    >>> list(repeat("God",times=3))
    ['God', 'God', 'God']

    二.累计迭代器

    itertools模块的accumulate迭代器用于返回累计和:    accumulate(iterable,[func])

    其中,若可迭代对象iterable的元素p0,p1,p2...为数值,则结果为:p0,p0+p1,p0+p1+p2....,如果指定了带两个参数的func,则func代替默认的加法运算

    >>> import itertools,operator
    >>> list(accumulate((1,2,3,4,5)))
    [1, 3, 6, 10, 15]
    >>> list(accumulate((1,2,3,4,5),operator.mul))
    [1, 2, 6, 24, 120]

    三.级联迭代器 chain(用于返回级联元素)

    chain(*iterable) #构造函数  连接所有的可迭代对象iterable1,iterable2..,即连接多个可迭代对象的元素,作为一个系列  chain('ABC', 'DEF') --> F

    chain的类工厂函数chain.from_iterable(iterable),也可以连接多个序列,,,chain.from_iterable(['ABC', 'DEF']) --> EF

    >>> import itertools
    >>> list(chain((1,2,3),"abc",range(5)))
    [1, 2, 3, 'a', 'b', 'c', 0, 1, 2, 3, 4]
    >>> list(chain.from_iterable(["ABC","DEF"]))
    ['A', 'B', 'C', 'D', 'E', 'F']

    四.选择压缩迭代器compress (用于返回可迭代对象的部分元素) compress(data,selectors)   compress('ABCDEF', [1,0,1,0,1,1]) --> F

     根据选择器selectors的元素(True/False),返回元素为True对应的data系列中的元素,当data或selectors终止时,停止判断

    五.切片迭代器islice(用于返回可迭代对象的切片)  islice(iterable,stop)   islice(iterable, [start,] stop [, step]) #构造函数  islice('ABCDEFG', 2, None) --> G

    islice返回可迭代对象iterable的切片,从索引位置start(第一个元素为0)开始,到stop(不包括)结束,步长为step(默认为1)。如果stop为None,则直至结束

    >>> import itertools
    >>> list(islice("ABCDEF",2))
    ['A', 'B']
    >>> list(itertools.islice("ABCDFEG",2))
    ['A', 'B']
    >>> list(itertools.islice("ABCDFEG",2,4))
    ['C', 'D']
    >>> list(itertools.islice("ABCDFEG",2,4,None))
    ['C', 'D']
    >>> list(itertools.islice("ABCDFEG",2,None))
    ['C', 'D', 'F', 'E', 'G']
    >>> list(itertools.islice("ABCDFEG",0,None,2))
    ['A', 'C', 'F', 'G']

    六.迭代器groupby (用于返回可迭代对象的分组)  groupby(iterable,key=None) #构造函数

    其中,iterable分带分组的可迭代对象,可选的key为用于计算键值的函数,默认为None,即键值为元素本身。groupby返回的结果为迭代器,其元素为(key,group),其中key是分组的键值,group为iterable中具有相同的key值得元素集合的子迭代器

    groups = []
    uniquekeys = []
    data = sorted(data, key=keyfunc)
    for k, g in groupby(data, keyfunc):
        groups.append(list(g))      # Store group iterator as a list
        uniquekeys.append(k)
    
    >>> from itertools import *
    >>> data=[1,-2,0,0,-1,-2,1,-1,2,0,0]
    >>> data1=sorted(data,key=abs)
    >>> for k,g in itertools.groupby(data1,key=abs):
        print(k,list(g))
    
        
    0 [0, 0, 0, 0]
    1 [1, -1, 1, -1]
    2 [-2, -2, 2]

    七.排列迭代器 permutations(用于系列的排列) permutations(iterable,r=None) #构造函数   返回可迭代对象iterable的元素排列,组合长度为r(默认为系列的长度)

    permutations('ABCD', 2)   AB AC AD BA BC BD CA CB CD DA DB DC

    >>> import itertools
    >>> list(itertools.permutations([1,2,3],2))
    [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
    >>> list(permutations([1,2,3]))
    [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

    八.笛卡尔积迭代器product(用于系列的笛卡尔积) product(*itertools,repeat=1)  product('ABCD', repeat=2)  AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD

    返回可迭代对象itertools1,itertools2...的元素的笛卡尔积,repeat为可迭代对象的重复次数(默认为1)

    >>> import itertools
    >>> list(itertools.permutations([1,2,3],2))
    [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
    >>> list(permutations([1,2,3]))
    [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
    >>> import itertools
    >>> list(itertools.product([1,2],"abc"))
    [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c')]
    >>> list(product(range(2),repeat=3))
    [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
  • 相关阅读:
    servlet之中文乱码:request.getParameter()
    html页面标题增加图标方法
    前端页面设计素材网站
    修电脑大全,学会不求人
    十篇TED点击率最高演讲,带你重新认识大数据与人工智能
    除了乔布斯,科技圈还有哪些大佬值得充信仰?
    快速建设网站的框架
    前端框架资源汇总
    获取页面跳转携带参数问题
    值得关注的博客和网站
  • 原文地址:https://www.cnblogs.com/Jdrops/p/5355174.html
Copyright © 2011-2022 走看看