Comparing a lot of data efficiently

Ignacio Vazquez-Abrams ignacio at openservices.net
Mon Aug 27 06:10:35 EDT 2001


On Mon, 27 Aug 2001, Marcus Stojek wrote:

> Any idea how I could significantly improve perfomance? In "Programming
> Python" I learned about tuple stacks, binary search trees and graphs but
> I'm not sure whether one of those will help. Do I really have to creep
> back to C? If so, can anyone tell me how to get my dictonaries into a C
> routine? (I know I have to use SWIG, but a little example would help a
> lot.)
>
> Thanks in advance.
>
> Marcus

Keys in dictionaries don't have to be strings or integers; they simply have to
be able to hash. Guess what? Tuples can hash :)

If you can guarantee that the nodes appear in the same order, then change your
dictionary definitions to be:

  {((x1, y1, z1), (x2, y2, z2), (x3, y2, z3)):num}

and then you can use the following:

  filter(trione.has_key, tritwo.keys())

to get all the common triangles of both.

If you can't guarantee that they're in the same order, then you'll have to
create some sort of class to hold the three tuples and return a value from
__hash__() that will be the same when the three nodes are in any order, but
not for nodes that have different values.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>








More information about the Python-list mailing list