[portland] Nested for loops, 4 levels deep

Dylan Reinhardt python at dylanreinhardt.com
Wed May 21 16:10:30 CEST 2008


On Wed, May 21, 2008 at 6:42 AM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> On Tue, 20 May 2008, Dylan Reinhardt wrote:
>> for v in varList:
>>   if row[0] == v[2] and s[1] == v[3]:
>>       ivr.write(v[0])
>>       ivr.write('\n''\n')
>>       subitems = [tuple(item[-2:]) for item in varList if item[:3] ==
>> v[:3]]
>>       for item in subitems:
>>          ivr.write('\t%s\t%s\n' % item)
>>
>> Untested, obviously... but maybe that's close enough to be useful?
>
>  I understand the list comprehension, but my modifications don't help. The
> multiple pairs at the lowest level are in appData.rules, not in v[]. I tried
> several variants, the latest being:
>
> subitems = [tuple(item[-2:]) for item in appData.rules if appData.rules[2]
> == v[0]]

OK... I can see that I got the appData part wrong... but I'm pretty
sure you're going to want to select table items by comparing a range
to a range, aren't you?

You're interested in all the n's and t's that share the same c, s, and
v.   How about this:

subitems = [tuple(item[-2:]) for item in appData.rules if item[:3] ==
(c[1], s[1], v[0])]

I'm not sure I extracted c, s, and v correctly... I'm getting a little
lost in there.  But the data you want consists of all the rows in
appData.rules where the first three elements match c, s, and v.  This
might not be the *exact* thing you're looking for, but should provide
the correct answer once the correct comparison is made.

HTH,

Dylan


More information about the Portland mailing list