recursive function

Max Erickson maxerickson at gmail.com
Mon Jan 8 14:05:06 EST 2007


"cesco" <fd.calabrese at gmail.com> wrote:

> 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
> 

This thread is probably of interest:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/f0c0
406fce981a54/59a2a5dcd1507ab9#59a2a5dcd1507ab9


max




More information about the Python-list mailing list