• Welcome to Valhalla Legends Archive.
 

Traversing Lists of Lists

Started by shadypalm88, May 30, 2007, 06:29 PM

Previous topic - Next topic

shadypalm88

Hi, guys. I'm racking my brain trying to figure out a generic way to do what the following Python code does for lists of three lists. I could just be stressed out and lacking sleep, but I don't see a good way!

possibles = ['ABCDE', '12345', 'XYZ']

def compute_paths(lst):
paths = []

for a in lst[0]:
for b in lst[1]:
for c in lst[2]:
paths.append([a, b, c])

return paths

for path in compute_paths(possibles):
print ''.join(path)


This correctly outputs:
A1X
A1Y
A1Z
A2X
A2Y
...
E4Y
E4Z
E5X
E5Y
E5Z


but I need to do it for any complexity. Thoughts?

Kp

How about this?  The level of indirection for the resulting list is a bit off, but that's a minor detail that you can fix if you care.


l = ['AB', '12', 'XZ']

def increment(i, c):
while c >= 0:
i[c] = i[c] + 1
if i[c] >= len(l[c]):
i[c], c = 0, c - 1
else:
break
return c

if len(l) <= 0:
sys.exit(0)
r = range(0, len(l))

p = []

i = [0 for c in r]
while True:
p.append([l[c][i[c]] for c in r])
if increment(i, c) < 0:
break
print p


Sample output:
[['A', '1', 'X'], ['A', '1', 'Z'], ['A', '2', 'X'], ['A', '2', 'Z'], ['B', '1', 'X'], ['B', '1', 'Z'], ['B', '2', 'X'], ['B', '2', 'Z']]
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Insolence

Not sure about any specific Python code, but you definitely want a recursive function--I think.

MyndFyre

Quote from: Insolence on May 30, 2007, 11:06 PM
Not sure about any specific Python code, but you definitely want a recursive function--I think.

Recursion, but Python has access to functional constructs that would make code a bit more elegant than procedural recursion.  :) 

But yes, recursion should be the way to go in this case, unless you're going to be nested on more than a couple orders of magnitude of complexity. ;)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Banana fanna fo fanna


possibles = ["ABCDE","12345","XYZ"]

def compute_paths(lst):
if len(lst) == 1:
return [[x] for x in lst[0]]
else:
r = []
for tail in compute_paths(lst[1:]):
for e in lst[0]:
r.append([e] + tail)
return r

for path in compute_paths(possibles):
print "".join(path)


Joe[x86]

Quote from: Insolence on May 30, 2007, 11:06 PM
Not sure about any specific Python code, but you definitely want a recursive function--I think.

You realize that's an oxymoron, right? :)
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

squeegee

Quote from: Joex86] link=topic=16744.msg169606#msg169606 date=1180734812]
Quote from: Insolence on May 30, 2007, 11:06 PM
Not sure about any specific Python code, but you definitely want a recursive function--I think.

You realize that's an oxymoron, right? :)

Python has some functional programming aspects, but it's not like say Haskell