How to get the length of an itertools.product?

前端 未结 4 982
無奈伤痛
無奈伤痛 2021-02-18 18:14

I am using itertools to run a numerical simulation iterating over all possible combinations of my input parameters. In the example below, I have two parameters and

相关标签:
4条回答
  • 2021-02-18 18:47

    Alternative solution I used:

    import itertools
    
    param = (('a', 'b'), (1, 2)) # a list of lists
    
    # Calculate all combinations
    combinations = itertools.product(*param)
    
    # Calculate number of combinations
    total_combinations = 1
    for i in param:
        total_combinations = total_combinations * len(i)
    
    0 讨论(0)
  • 2021-02-18 18:54

    To implement Kevin's answer for an arbitrary number of source iterables, combining reduce and mul:

    >>> import functools, itertools, operator
    >>> iters = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> functools.reduce(operator.mul, map(len, iters), 1)
    27
    >>> len(list(itertools.product(*iters)))
    27
    

    Note that this will not work if your source iterables are themselves iterators, rather than sequences, for the same reason your initial attempts to get the length of the itertools.product failed. Python generally and itertools specifically can work in a memory-efficient way with iterators of any length (including infinite!) so finding out lengths up-front isn't really a case it was designed to deal with.

    0 讨论(0)
  • 2021-02-18 18:55

    How about:

    mylength = len(x) * len(y)
    
    0 讨论(0)
  • 2021-02-18 19:11

    While this doesn't answer the question directly, very often we want to find the length of generators to estimate the progress/runtime.

    For this, do consider using tqdm's (version >= 4.42.0) wrappers around generator functions that don't forget the lengths of iterators (tqdm is a progressbar library). E.g.,

    from tqdm.contrib.itertools import product
    from time import sleep
    for i, j in product(range(3), range(4)):
        sleep(1)
    

    will show a progress bar. The length of the product is shown as the total of the tqdm object (e.g.., the 6 in 3/6 [00:03<00:03] shown).

    0 讨论(0)
提交回复
热议问题