creating a multidimensional array list from a function

You can use itertools.product to generate all the different combinations of indexes from the ranges. You can then iterate over the tuples produced by that iterator, calling f for each tuple. For example, if f is defined to return a string of the input indexes:

import itertools

def f(indexes):
    return ','.join(map(str, indexes))
ranges = [range(0, 2), range(1, 3), range(2, 4)]

objs = [f(list(t)) for t in itertools.product(*ranges)]


['0,1,2', '0,1,3', '0,2,2', '0,2,3', '1,1,2', '1,1,3', '1,2,2', '1,2,3']

Note that dependent on your implementation of f, it might not be necessary to convert the returned tuple from itertools.product to a list and you could just use f(t) instead of f(list(t)).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top