[Python] coding help -- iterating over 2 arrays by individual
orderings
Rob Hudson
rob at euglug.net
Thu Jan 11 13:42:51 PST 2007
Fellow Pythonistas,
I'm looking for a little Python style help. I'm using Django to gather
2 different types of content items. Each item has a sequence order
associated with it. For reasons associated with how the data model is
laid out, I can't combine these queries into a single one.
So I have an array of text items and an array of media items.
I want to iterate over them in the order specified by text.item.sequence
and media.item.sequence. Individually text is sorted by sequence and so
is media.
One thought was to throw them in a dict with the sequence being the key
and the object being the value and iterate using a key sort. But if the
content teams goof and input duplicate sequence items content gets lost.
Another thought was to iterate and check if text.item.sequence is
greater than media.item.sequence and pull which ever one is lower.
That seems ok to me but I'm thinking this is a common enough thing that
there's probably a Pythonic way of doing it that I'm not aware of. Is
there? :)
Thanks,
Rob
More information about the Python
mailing list