zoukankan      html  css  js  c++  java
  • Python内置库itertools简单学习

    该库为满足特定需要的比较高效的迭代器内置库,在数据科学中的应用也不少,故有必要了解一下:

    import itertools
    
    import sys
    

    无限迭代器(Infinite iterators)

    Iterator Arguments Results Example
    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
    • itertools.count
    a=itertools.count(5)
    
    type(a)
    
    itertools.count
    
    next(a)
    
    5
    
    next(a)
    
    6
    
    next(a)
    
    7
    
    b=itertools.count(6,2)
    
    next(b)
    
    6
    
    next(b)
    
    8
    
    • itertools.cycle
    a=itertools.cycle([1,2])
    
    next(a)
    
    1
    
    next(a)
    
    2
    
    next(a)
    
    1
    
    next(a)
    
    2
    
    b=itertools.cycle('ABC')
    
    next(b)
    
    'A'
    
    next(b)
    
    'B'
    
    next(b)
    
    'C'
    
    next(b)
    
    'A'
    
    • itertools.repeat
    a=itertools.repeat([1,2,3])
    
    next(a)
    
    [1, 2, 3]
    
    next(a)
    
    [1, 2, 3]
    
    b=itertools.repeat('ABC',3)
    
    next(b)
    
    'ABC'
    
    next(b)
    
    'ABC'
    
    next(b)
    
    'ABC'
    
    try:
        next(b)
    except Exception as e:
        print(sys.exc_info())
    
    (<class 'StopIteration'>, StopIteration(), <traceback object at 0x0000024CB3EE2E08>)
    

    最短输入停止迭代器(Iterators terminating on the shortest input sequence)

    Iterator Arguments Results Example
    accumulate() p [,func] p0, p0+p1, p0+p1+p2, … accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    chain() p, q, … p0, p1, … plast, q0, q1, … chain('ABC', 'DEF') --> A B C D E F
    chain.from_iterable() iterable p0, p1, … plast, q0, q1, … chain.from_iterable(['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
    filterfalse() pred, seq elements of seq where pred(elem) is false filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
    groupby() iterable[, key] sub-iterators grouped by value of key(v)
    islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) --> C D E F G
    starmap() func, seq func(seq[0]), func(seq[1]), … starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    takewhile() pred, seq seq[0], seq[1], until pred fails takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    tee() it, n it1, it2, … itn splits one iterator into n
    zip_longest() p, q, … (p[0], q[0]), (p[1], q[1]), … zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    • itertools.accumulate
    a=itertools.accumulate([1,2,3,4])
    
    list(a)
    
    [1, 3, 6, 10]
    
    b=itertools.accumulate('abc')
    
    list(b)
    
    ['a', 'ab', 'abc']
    
    • itertools.chain
    a=itertools.chain([1,2],[3,4],[5,6])
    
    list(a)
    
    [1, 2, 3, 4, 5, 6]
    
    b=itertools.chain('ab','cd')
    
    list(b)
    
    ['a', 'b', 'c', 'd']
    
    • itertools.chain.from_iterable
    a=itertools.chain.from_iterable(['abc','def'])
    
    list(a)
    
    ['a', 'b', 'c', 'd', 'e', 'f']
    
    • itertools.compress
    a=itertools.compress('abcdef',[1,0,1,0,1,1])
    
    list(a)
    
    ['a', 'c', 'e', 'f']
    
    a=itertools.compress('abcdef',[1,False,1,False,1,0])
    
    list(a)
    
    ['a', 'c', 'e']
    
    • itertools.dropwhile
    a=itertools.dropwhile(lambda x:x<5,[1,4,6,4,1])
    
    list(a)
    
    [6, 4, 1]
    
    • itertools.takewhile
    a=itertools.takewhile(lambda x:x<5,[1,4,6,4,1])
    
    list(a)
    
    [1, 4]
    
    • filterfalse
    a=itertools.filterfalse(lambda x:x%2,range(10))
    
    list(a)
    
    [0, 2, 4, 6, 8]
    
    • groupby
    x = [(1, 2), (2, 3), (1, 4), (5, 5), (3, 4), (2, 6)]
    
    a=itertools.groupby(x,lambda x:x[0])
    
    for i in a:
        print(i[0],list(i[1]))
    
    1 [(1, 2)]
    2 [(2, 3)]
    1 [(1, 4)]
    5 [(5, 5)]
    3 [(3, 4)]
    2 [(2, 6)]
    
    • itertools.islice
    a=itertools.islice('abcdefg',2,None)
    
    list(a)
    
    ['c', 'd', 'e', 'f', 'g']
    
    b=itertools.islice('abcdefg',2,4)
    
    list(b)
    
    ['c', 'd']
    
    • itertools.starmap
    a=itertools.starmap(pow,[(2,5),(3,2)])
    
    list(a)
    
    [32, 9]
    
    • itertools.tee
    a=itertools.tee([1,2,3],2)
    
    list(a)
    
    [<itertools._tee at 0x24cb3f29448>, <itertools._tee at 0x24cb3f29888>]
    
    a=itertools.tee([1,2,3],2)
    
    list(a[0])
    
    [1, 2, 3]
    
    list(a[1])
    
    [1, 2, 3]
    
    • itertools.zip_longest
    a=itertools.zip_longest('abcd','xy',fillvalue='-')
    
    list(a)
    
    [('a', 'x'), ('b', 'y'), ('c', '-'), ('d', '-')]
    

    组合迭代器

    Iterator Arguments Results
    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
    a=itertools.product([1,2,3],[4,5,6])
    
    list(a)
    
    [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
    
    a=itertools.product([1,2,3],[4,5,6],repeat=2)
    
    list(a)
    
    [(1, 4, 1, 4),
     (1, 4, 1, 5),
     (1, 4, 1, 6),
     (1, 4, 2, 4),
     (1, 4, 2, 5),
     (1, 4, 2, 6),
     (1, 4, 3, 4),
     (1, 4, 3, 5),
     (1, 4, 3, 6),
     (1, 5, 1, 4),
     (1, 5, 1, 5),
     (1, 5, 1, 6),
     (1, 5, 2, 4),
     (1, 5, 2, 5),
     (1, 5, 2, 6),
     (1, 5, 3, 4),
     (1, 5, 3, 5),
     (1, 5, 3, 6),
     (1, 6, 1, 4),
     (1, 6, 1, 5),
     (1, 6, 1, 6),
     (1, 6, 2, 4),
     (1, 6, 2, 5),
     (1, 6, 2, 6),
     (1, 6, 3, 4),
     (1, 6, 3, 5),
     (1, 6, 3, 6),
     (2, 4, 1, 4),
     (2, 4, 1, 5),
     (2, 4, 1, 6),
     (2, 4, 2, 4),
     (2, 4, 2, 5),
     (2, 4, 2, 6),
     (2, 4, 3, 4),
     (2, 4, 3, 5),
     (2, 4, 3, 6),
     (2, 5, 1, 4),
     (2, 5, 1, 5),
     (2, 5, 1, 6),
     (2, 5, 2, 4),
     (2, 5, 2, 5),
     (2, 5, 2, 6),
     (2, 5, 3, 4),
     (2, 5, 3, 5),
     (2, 5, 3, 6),
     (2, 6, 1, 4),
     (2, 6, 1, 5),
     (2, 6, 1, 6),
     (2, 6, 2, 4),
     (2, 6, 2, 5),
     (2, 6, 2, 6),
     (2, 6, 3, 4),
     (2, 6, 3, 5),
     (2, 6, 3, 6),
     (3, 4, 1, 4),
     (3, 4, 1, 5),
     (3, 4, 1, 6),
     (3, 4, 2, 4),
     (3, 4, 2, 5),
     (3, 4, 2, 6),
     (3, 4, 3, 4),
     (3, 4, 3, 5),
     (3, 4, 3, 6),
     (3, 5, 1, 4),
     (3, 5, 1, 5),
     (3, 5, 1, 6),
     (3, 5, 2, 4),
     (3, 5, 2, 5),
     (3, 5, 2, 6),
     (3, 5, 3, 4),
     (3, 5, 3, 5),
     (3, 5, 3, 6),
     (3, 6, 1, 4),
     (3, 6, 1, 5),
     (3, 6, 1, 6),
     (3, 6, 2, 4),
     (3, 6, 2, 5),
     (3, 6, 2, 6),
     (3, 6, 3, 4),
     (3, 6, 3, 5),
     (3, 6, 3, 6)]
    
    • itertools.permutations
    for i in itertools.permutations([1,2,3],2):
        print (i)
    
    (1, 2)
    (1, 3)
    (2, 1)
    (2, 3)
    (3, 1)
    (3, 2)
    
    for i in itertools.permutations('123',2):
        print (i)
    
    ('1', '2')
    ('1', '3')
    ('2', '1')
    ('2', '3')
    ('3', '1')
    ('3', '2')
    
    for i in itertools.permutations('122',2):
        print (i)
    
    ('1', '2')
    ('1', '2')
    ('2', '1')
    ('2', '2')
    ('2', '1')
    ('2', '2')
    
    • itertools.combinations
    a=itertools.combinations([1,2,3],2)
    
    list(a)
    
    [(1, 2), (1, 3), (2, 3)]
    
    a=itertools.combinations('abcd',2)
    
    list(a)
    
    [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
    
    • itertools.combinations_with_replacement
    a=itertools.combinations_with_replacement('abcd',2)
    
    list(a)
    
    [('a', 'a'),
     ('a', 'b'),
     ('a', 'c'),
     ('a', 'd'),
     ('b', 'b'),
     ('b', 'c'),
     ('b', 'd'),
     ('c', 'c'),
     ('c', 'd'),
     ('d', 'd')]
    ##### 愿你一寸一寸地攻城略地,一点一点地焕然一新 #####
  • 相关阅读:
    搭建vue开发环境的步骤
    widow怎么结束某端口占用
    替换数据库表中某个代码段中某个字段的某一段字符串sql
    sql优化
    scssmap对象访问
    IntelliJ IDEA 和 webstorm更换主题
    css滚动条样式
    redis set
    【LeetCode】128. 最长连续序列
    第二章 模型评估
  • 原文地址:https://www.cnblogs.com/johnyang/p/13792155.html
Copyright © 2011-2022 走看看