Combining arbitrary lists

Nick nchackowsky at gmail.com
Sun Nov 14 22:28:10 EST 2004


Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
   for y in n[1]:
     for z in n[2]:
       print [x, y, z]

--output--
[1, 4, 7]
[1, 4, 8]
[1, 5, 7]
[1, 5, 8]
  ...
[3, 6, 8]
--      --

How can I do this for an arbirary length of n? This reminds me of those 
horrible fraction questions in 1st year comp. sci.
x = 1 + 1/(1 + 1/(1 + 1/(..... )))))... which leads me to suspect that 
the simplest solution is recursive... hmmm... I'll take any suggestions.



Muchos Gracias
Nick.



More information about the Python-list mailing list