matching objects by a tuple field criterion

Diez B. Roggisch deets at nospam.web.de
Sun Jun 10 08:12:57 EDT 2007


bullockbefriending bard schrieb:
>> Instead of passing a wild-card tuple like (*,*,*,4,*,*) simply pass the
>> integer you want to match and the position you want to match it in.
> 
> for sure. that was more for expository purpose rather than how i was
> planning to go about it.
> 
> 
>> As a generator expression:
>>
>> (obj for obj in list_of_objects if obj.data[what] == where)
> 
> above or equivalent list comprehension was what i had in mind as far
> as linear search goes. and scanning the list like this will most
> likely be 'good enough' performance-wise. however, partly just out of
> curiosity, i was wondering if there is some kind of data structure
> which might let me find all the matches a bit faster.

You can of course create a tree from the tuples, where the first level 
of nodes corresponds to the first attribute of the tuple and so forth.

There are certainly cases where the speedup is tremendous - think of a 
single integer in the first criteria - but then the overall performance 
depends on the real-live queries. If lot's of wildcards are used, you 
might end up slower if the tree-walk takes more time than the 
C-implemented list-iteration will cost you.

Diez



More information about the Python-list mailing list