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

Rob Hudson rob at euglug.net
Thu Jan 11 14:31:12 PST 2007


Quentin Hartman wrote:
> I don't that I have enough information about what you are doing to
> make suggestions, but it sounds like you want to have both the text
> and media items to have something done to them in order, according to
> their sequence number, correct? If that's the case (and depending on
> how the lists/dictionaries/whatever are originally built) I would feed
> them both into one dictionary and then sort that by the sequence, and
> then just iterate over the whole thing. The built-in sort function is
> known to be pretty fast, and this method would save a lot of
> extraneous comparisons. It would probably be easier logic to manage as
> well.
> 

The only thing I don't like about this is that the dictionary can only 
have 1 key.  But if the user repeats the sequence number, then the 
program appears to not work.

I could possibly enforce unique sequences at the DB layer by using a 
multi-column unique key.  That's probably a good option.

Otherwise I did find this in Django...

In [45]: from django.utils.datastructures import MultiValueDict

In [46]: d = MultiValueDict({1: ['text'], 2: ['text','media'], 3: ['text']})

In [47]: d
Out[47]: <MultiValueDict: {1: ['text'], 2: ['text', 'media'], 3: ['text']}>

In [48]: d[1]
Out[48]: 'text'

In [49]: d[2]
Out[49]: 'media'

In [50]: d.getlist(2)
Out[50]: ['text', 'media']

In [51]: d.items()
Out[51]: [(1, 'text'), (2, 'media'), (3, 'text')]

In [52]: d.lists()
Out[52]: [(1, ['text']), (2, ['text', 'media']), (3, ['text'])]


More information about the Python mailing list