recursive function

cesco fd.calabrese at gmail.com
Mon Jan 8 15:07:20 CET 2007


Hi,

I have a dictionary of lists of tuples like in the following example:
dict = {1: [(3, 4), (5, 8)],
        2: [(5, 4), (21, 3), (19, 2)],
        3: [(16, 1), (0, 2), (1, 2), (3, 4)]]

In this case I have three lists inside the dict but this number is
known only at runtime. I have to write a function that considers all
the possible combinations of tuples belonging to the different lists
and return a list of tuples of tuples for which the sum of the first
element of the most inner tuple is equal to N.

For example, assuming N = 24, in this case it should return:
[((3, 4), (5, 4), (16, 1)), ((3, 4), (21, 3), (0, 2)), ((5, 8), (19,
2), (0, 2))]

A simple list comprehension would be enough if only I knew the number
of keys/lists beforehand but this is not the case. I guess I need a
recursive function. Can anyone help?

Thanks in advance
Francesco




More information about the Python-list mailing list