How to get the length of an itertools.product?

和自甴很熟 提交于 2020-04-10 07:01:22

问题


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 six possible combinations:

import itertools

x = [0, 1]
y = [100, 200, 300]

myprod = itertools.product(x, y)

for p in myprod:
    print p[0], p[1]
    # run myfunction using p[0] as the value of x and p[1] as the value of y

How can I get the size of myprod (six, in the example)? I'd need to print this before the for loop starts.

I understand myprod is not a list. I can calculate len(list(myprod)), but this consumes the iterator so the for loop no longer works.

I tried:

myprod2=copy.deepcopy(myprod)
mylength = len(list(myprod2))

but this doesn't work, either. I could do:

myprod2=itertools.product(x,y)
mylength = len(list(myprod2))

but it's hardly elegant and pythonic!


回答1:


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.




回答2:


How about:

mylength = len(x) * len(y)



回答3:


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)


来源:https://stackoverflow.com/questions/32074543/how-to-get-the-length-of-an-itertools-product

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!