[portland] Nested for loops, 4 levels deep

Rich Shepard rshepard at appl-ecosys.com
Wed May 21 00:40:39 CEST 2008

   I have been trying since yesterday morning to figure out how to nest for
loops four levels deep. I've tried the suggestions offered by you folks as
solutions to my previous needs in working with lists of tuples. This time
I've had no problems getting three deep, but I cannot successfully (and
properly) extract the last level. I'm confident that you'll show me several
different ways of getting the desired result.

   I start with a list of tuples called appData.rules. Each tuple consists of
5 strings; we can refer to them as 'c,' 's,' 'v,' 'n,' and 't.' What I need
is to print the hierarchies. That is, for each 'c' print all the 's'
associated with it; for each 's' print each of its associated 'v'; for each
'v' print its associated 'n' and 't' in a two-column table

   Earlier in the module I define three lists of tuples: compList, subList,
and varList. I use these to print the hierarchies of the first three items
in each tuple.

   Here's the working code:

   proper = lambda t: (t[0], t[1], t[2], t[3])



   for c in compList:
     curComp = None
     for row in appData.rules:
       if row[0] == c[1] and row[0] != curComp:
         ivr.write('\\subsection{%s}' % c[1])
         for s in subList:
           if row[0] == s[0]:
             ivr.write('\\subsubsection{%s}' % s[1])
             for v in varList:
               if row[0] == v[2] and s[1] == v[3]:
 		in here is where I want to create a table for rows 3
 		and 4, where they belong to the specific c, s, and v
         curComp = row[0]

   A page of the output is attached.

   I can get only 1 n/t pair printing for each c/s/v combination, or I have
all n/t pairs repeating. I probably need another for loop, but those I've
tried failed miserably.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: inputVars.pdf
Type: application/pdf
Size: 13528 bytes
URL: <http://mail.python.org/pipermail/portland/attachments/20080520/3092c792/attachment.pdf>

More information about the Portland mailing list