[Python] coding help -- iterating over 2 arrays by individual orderings

Rob Hudson rob at euglug.net
Fri Jan 12 09:02:25 PST 2007


Bob Miller wrote:
> Is that not Pythonic enough for you? (-:  Perhaps this is.
> 
>     from itertools import chain
>     for content in sorted(chain(text, media), key=lambda i: i.seq):
>         display_text_or_media(content)
> 

I can't use sorted(), unfortunately, b/c it's Python 2.4.  Our CentOS 
servers are still on Python 2.3 for now.  (I'm patiently awaiting the 
upgrade myself.)

What I've ended up with currently is this:

     items = {}
     for item in chain(text, media):
         items[item.item.sequence] = item
     keys = items.keys()
     keys.sort()
     for k in keys:
         item = items[k]

Plus I added in the 2-column key so there can be not content items on 
the same page with the same sequence.  So the original goal is gone -- 
there should be no content with the same sequence.

Thanks,
Rob


More information about the Python mailing list