zoukankan      html  css  js  c++  java
  • 【385】itertools 的 product 和 chain 和 accumulate

    参考:itertools模块

    product

    相当于返回两个集合中数据的所有组合可能

    Examples from Eric Martin

    from itertools import product
    
    print(list(product([0, 1], 'abc')))
    print()
    print(list(product(['A', 'B'], ('a', 'b'), range(2))))
    print()
    print(list(product([0, 1], repeat = 2)))
    print()
    print(list(product('ab', repeat = 4)))
    
    output:
    [(0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c')]
    
    [('A', 'a', 0), ('A', 'a', 1), ('A', 'b', 0), ('A', 'b', 1), ('B', 'a
    ', 0), ('B', 'a', 1), ('B', 'b', 0), ('B', 'b', 1)]
    
    [(0, 0), (0, 1), (1, 0), (1, 1)]
    
    [('a', 'a', 'a', 'a'), ('a', 'a', 'a', 'b'), ('a', 'a', 'b', 'a'), ('
    a', 'a', 'b', 'b'), ('a', 'b', 'a', 'a'), ('a', 'b', 'a', 'b'), ('a',
    'b', 'b', 'a'), ('a', 'b', 'b', 'b'), ('b', 'a', 'a', 'a'), ('b', 'a
    ', 'a', 'b'), ('b', 'a', 'b', 'a'), ('b', 'a', 'b', 'b'), ('b', 'b',
    'a', 'a'), ('b', 'b', 'a', 'b'), ('b', 'b', 'b', 'a'), ('b', 'b', 'b'
    , 'b')]
    
    from itertools import product
    a = (1, 2, 3)
    b = ('A', 'B', 'C')
    c = ('d', 'e', 'f')
    pros = product(a, b, c)
    count = 1
    for elem in pros:
        print(f'{count:02}', "---", elem)
        count+=1
    
    output:
    01 --- (1, 'A', 'd')
    02 --- (1, 'A', 'e')
    03 --- (1, 'A', 'f')
    04 --- (1, 'B', 'd')
    05 --- (1, 'B', 'e')
    06 --- (1, 'B', 'f')
    07 --- (1, 'C', 'd')
    08 --- (1, 'C', 'e')
    09 --- (1, 'C', 'f')
    10 --- (2, 'A', 'd')
    11 --- (2, 'A', 'e')
    12 --- (2, 'A', 'f')
    13 --- (2, 'B', 'd')
    14 --- (2, 'B', 'e')
    15 --- (2, 'B', 'f')
    16 --- (2, 'C', 'd')
    17 --- (2, 'C', 'e')
    18 --- (2, 'C', 'f')
    19 --- (3, 'A', 'd')
    20 --- (3, 'A', 'e')
    21 --- (3, 'A', 'f')
    22 --- (3, 'B', 'd')
    23 --- (3, 'B', 'e')
    24 --- (3, 'B', 'f')
    25 --- (3, 'C', 'd')
    26 --- (3, 'C', 'e')
    27 --- (3, 'C', 'f')
    

    例子2:二进制数三位数的所有可能

    a = (0, 1)
    b = (0, 1)
    c = (0, 1)
    pros = product(a, b, c)
    count = 1
    for elem in pros:
        print(f'{count:02}', "---", elem)
        count+=1
    
    output:
    01 --- (0, 0, 0)
    02 --- (0, 0, 1)
    03 --- (0, 1, 0)
    04 --- (0, 1, 1)
    05 --- (1, 0, 0)
    06 --- (1, 0, 1)
    07 --- (1, 1, 0)
    08 --- (1, 1, 1)
    

    chain 就是合并成一个 iter

    from itertools import chain
    [e for e in chain([2, 3], {3, 4}, (3,4))]
    
    output:
    [2, 3, 3, 4, 3, 4]
    

    accumulate 可以实现将可迭代对象进行累加的效果,形成一个新的可迭代对象

    >>> a = accumulate([1, 2, 3, 4])
    
    >>> [i for i in a]
    [1, 3, 6, 10]
    
  • 相关阅读:
    二分图那套理论
    洛谷P4351 [CERC2015]Frightful Formula【组合计数】
    「AGC023E」Inversions【组合计数】
    类欧几里得算法及其拓展
    OLAP 一些扯淡
    auto vectorized case shift
    备忘录
    lambda function pointer
    C++ atomic
    gdb 使用了 O0 但是还是有 <optimized out>
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/10571025.html
Copyright © 2011-2022 走看看