• python迭代器Itertools


    https://docs.python.org/3.6/library/itertools.html

    一无限迭代器:

    IteratorArgumentsResultsExample

    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') --> ...

    repeat()

    elem [,n]

    elem, elem, elem, ... endlessly or up to n times

    repeat(10, 3) --> 10 10 10  

    • itertools.count(arg1,arg2):arg1:开始位置,arg2:步长
     1 #相当于for i in range(10,2)
     2 
     3 #创建迭代器,从10开始步长为2,无结束
     4 
     5 >>> import itertools
     6 >>> n = itertools.count(10,2)
     7 >>> print(type(n))
     8 <class 'itertools.count'>
     9 >>> for i in n:
    10     print(i)
    itertools.count()
    • itertools.cycle(itertable)
    1 生成一个无限循环可迭代参数的迭代器
    2 >>> itertools.cycle('ABCDE')
    3 <itertools.cycle object at 0x00000000033576C8>
    4 >>> for i in itertools.cycle('ABCDE'):
    5     print(i)
    6 
    7 #ABCDEABCDE.......
    itertools.cycle()
    • itertools.repeat(arg1,arg2):arg1:要重复的参数,arg2:重复多少遍
    1 >>> s = itertools.repeat('ABC',3)
    2 >>> s
    3 repeat('ABC', 3)
    4 >>> for i in s:
    5     print(i)
    6 #ABC
    7 #ABC
    8 #ABC
    itertools.repeat()

    二处理输入序列迭代器

    迭代器参数结果
    accumulate() p [,func] p0,p0 + p1,p0 + p1 + p2,... accumulate([1,2,3,4,5]) --> 610 15
    chain() p,q,... p0,p1,... plast,q0,q1,... chain('ABC', 'DEF') --> EF
    chain.from_iterable() 迭代 p0,p1,... plast,q0,q1,... chain.from_iterable(['ABC','DEF']) --> F
    compress() 数据,选择器 (d [0]如果s [0]),(d [1]如果s [1]),...... compress('ABCDEF', [1,0,1,0,1,1])--> 

    dropwhile()

    pred,seq seq [n],seq [n + 1],当pred失败时开始 dropwhile(lambda x: x<5,[1,4,6,4,1]) --> 1
    filterfalse() pred,seq seq的元素,其中pred(elem)是假的 filterfalse(lambda x: x%2,range(10)) --> 8
    groupby() 可迭代的[,关键] 按键值(v)分组的子迭代器  
    islice() seq,[start,] stop [,step] 来自seq [start:stop:step]的元素 islice('ABCDEFG', 2, None) --> CG
    starmap() func,seq func(* seq [0]),func(* seq [1]),... starmap(pow, [(2,5), (3,2),(10,3)]) --> 32 1000
    takewhile() pred,seq seq [0],seq [1],直到pred失败 takewhile(lambda x: x<5,[1,4,6,4,1]) --> 4
    tee() 它,n it1,it2,... itn将一个迭代器拆分为n  
    zip_longest() p,q,... (p [0],q [0]),(p [1],q [1]),... zip_longest('ABCD', 'xy',fillvalue='-') --> Ax By C- D-
    • itertools.accumulate(terable ,func )

        创建一个迭代器,返回累积的总和,或其他二进制函数的累计结果(通过可选的func参数指定 )。如果提供了func,它应该是两个参数的函数。输入可迭代的 元素可以是可以被接受为func的参数的任何类型,如果输入iterable为空,则输出iterable也将为空。

    1 >>> import operator
    2 >>> n=itertools.accumulate([1,2,3,4])
    3 >>> for i in n:
    4 ...     print(i)#1 3 6 10
    5 
    6 >>> n=itertools.accumulate([1,2,3,4],func=operator.mul)
    7 >>> for i in n:
    8 ...     print(i) #1 2 6 24
    itertools.accumulate()
    • itertools.chain(*iterable)
     1 将俩个可迭代对象缝合起来,生成一个迭代器
     2 >>> n=itertools.chain('abc','def')
     3 >>> for i in n:
     4 ...     print(i)
     5 ...
     6 a
     7 b
     8 c
     9 d
    10 e
    11 f
    itertools.chain()
    • classmethod chain.from_iterableiterable )对于一个可迭代的数据生成一个迭代器
    1 >>> for i in itertools.chain.from_iterable(["abc","def"]):
    2 ...     print(i)
    3 ...
    4 a
    5 b
    6 c
    7 d
    8 e
    9 f
    View Code
    • itertools.compress(data, selectors) :选择那些字符作为迭代器内容
    1 #选择那些字符输出
    2 >>> for i in itertools.compress("ABCDEFG",[1,0,1,1,1,0,0]):
    3 ...     print(i)
    4 ...
    5 A
    6 C
    7 D
    8 E
    itertools.compress()
    • itertools.dropwhile(pred,seq):当不满足pred条件时,截取后面的数据做为迭代器内容
    1 dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    View Code
    • itertools.groupby(iterable,key)

      返回一个产生按照key进行分组后的值集合的迭代器.

      如果iterable在多次连续迭代中生成了同一项,则会定义一个组,如果将此函数应用一个分类列表,那么分组将定义该列表中的所有唯一项,key(如果已提供)是一个函数,应用于每一项,如果此函数存在返回值,该值将用于后续项而不是该项本身进行比较,此函数返回的迭代器生成元素(key, group),其中key是分组的键值,group是迭代器,生成组成该组的所有项。

    1 >>> for key, group in itertools.groupby('AAABBBCCAAA'):
    2 ...     print(key, list(group))
    3 ...
    4 A ['A', 'A', 'A']
    5 B ['B', 'B', 'B']
    6 C ['C', 'C']
    7 A ['A', 'A', 'A']
    itertools.group()
    • itertools.starmap(func,seq)
    1 starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    View Code
    • itertools.tee(iterable,n)
     1 >>> for i in itertools.tee([1,2,3],3):
     2 ...     for x in i:
     3 ...             print(x)
     4 ...
     5 1
     6 2
     7 3
     8 1
     9 2
    10 3
    11 1
    12 2
    13 3
    View Code

    三:组合迭代器

    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
    • itertools.product(*iterables[, repeat]) 笛卡尔积

        创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数

     1 b=('a','b'.'c'),a=(1,2,3),c=('d','e','f')
     2 >>> c=itertools.product(a,b,c)
     3 >>> for i in c:
     4 ...     print(i)
     5 (1, 'a', 'd')
     6 (1, 'a', 'e')
     7 (1, 'a', 'f')
     8 (1, 'b', 'd')
     9 (1, 'b', 'e')
    10 (1, 'b', 'f')
    11 (1, 'c', 'd')
    12 (1, 'c', 'e')
    13 (1, 'c', 'f')
    14 (2, 'a', 'd')
    15 (2, 'a', 'e')
    16 (2, 'a', 'f')
    17 (2, 'b', 'd')
    18 (2, 'b', 'e')
    19 (2, 'b', 'f')
    20 (2, 'c', 'd')
    21 (2, 'c', 'e')
    22 (2, 'c', 'f')
    23 (3, 'a', 'd')
    24 (3, 'a', 'e')
    25 (3, 'a', 'f')
    26 (3, 'b', 'd')
    27 (3, 'b', 'e')
    28 (3, 'b', 'f')
    29 (3, 'c', 'd')
    30 (3, 'c', 'e')
    31 (3, 'c', 'f')
    View Code
    • itertools.permutation(iterable[, r]),按指定每个元素长度,返回所有有重复的排列,但不允许一个元素中有相同要的子元素
    1 >>> a = [1, 2, 3, 4]
    2 >>> s = [i for i in itertools.permutations(a,3)] # 从序列a中选出3个元素进行组合排列
    3 >>> s
    4 [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
    View Code
     1 >>> from itertools import permutations
     2 >>> print permutations(['1','2','3'])
     3 <itertools.permutations object at 0x02A45210>
     4 >>> 
     5 >>> print list(permutations(['1','2','3']))
     6 [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]
     7 >>> 
     8 >>> print list(permutations(['1','2','3'],2))
     9 [('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
    10 >>>
    11 >>> print list(permutations('abc',3))
    12 [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
    View Code
    • itertools.combinations(iterable, r) 按指定长度,返回所有无重复组合,且每个元素中的子元素不能重复(如AA不可以)
    1 >>> a = [1, 2, 3, 4]
    2 >>> s = [i for i in itertools.combinations(a,2)] # 从序列a中选出2个不重复的元素
    3 >>> s
    4 [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
    View Code
    •  itertools.combinations(iterable,r):和上面的一样,只是允许每个元素中的子元素可以相同(AA可以)
    1 >>> from itertools import combinations_with_replacement
    2 >>> 
    3 >>> print list(combinations_with_replacement('12345',2))
    4 [('1', '1'), ('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '2'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '3'), ('3', '4'), ('3', '5'), ('4', '4'), ('4', '5'), ('5', '5')]
    5 >>> 
    6 >>> A = [1,1,3,3,3]
    7 >>> print list(combinations(A,2))
    8 [(1, 1), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (3, 3), (3, 3), (3, 3)]
    View Code
  • 相关阅读:
    ASP.NET MVC 音乐商店 目录
    ASP.NET MVC 音乐商店 9. 注册和结账
    SQL查询入门(下篇)
    【译】Asp.net MVC并不仅仅只是Linq to SQL
    SQL查询入门(中篇)
    【译】利用.LESS来提高CSS
    Silverlight 入门学习笔记(1)Silverlight是什么
    【译】详解Asp.net MVC DropDownLists
    Asp.net缓存简介
    SQL查询入门(上篇)
  • 原文地址:https://www.cnblogs.com/Mr-l/p/10772430.html
Copyright © 2020-2023  润新知