Pythonic way to iterate through multidimensional space?

Ned Batchelder ned at nedbatchelder.com
Tue Oct 7 13:01:29 CEST 2014


On 10/7/14 2:10 AM, Gelonida N wrote:
>
> Disadvantage of itertools.product() is, that it makes a copy in memory.
> Reason ist, that itertools also makes products of generators (meaning of
> objects, that one can't iterate several times through)
>
>
> There are two use cases, that I occasionaly stumble over:
>
> One is making the product over lists(),
> product( list_of_lists )
>
> ex:
>
> product( [ [1,2,3], ['A','B'], ['a', 'b', 'c'] ] )
>
> the other one making a product over a list of functions, which will
> create generators
>
> ex:
> product( [ lambda: [ 'A', 'B' ], lambda: xrange(3) ] )
>
>
> I personally would also be interested in a fast generic solution that
> can iterate through an N-dimensional array and which does not duplicate
> the memory or iterate through a list of generator-factories or however
> this would be called.

itertools.product makes a copy of the sequences passed in, but it is a 
shallow copy. It doesn't copy the objects in the sequences.  It also 
doesn't store the entire product.

If you are calling product(j, k, l, m, n), where len(j)==J, the extra 
memory is J+K+L+M+N, which is much smaller than the number of iterations 
product will produce.  Are you sure that much extra memory use is a 
problem?  How large are your lists that you are product'ing together?

I don't understand your point about a list of functions that create 
generators?  What is the problem there?

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list