humanbeing humanbeing - 5 months ago 32
Python Question

Hash function and table implementation for over 50k entries in python

Can anyone give a good tutorial of how to create a hash table by hashing keys to values and avoiding/dealing with collisions in python? I have seen lots of little bits of code here and there, but I was wondering if someone could assist me.


  • Create the table

  • Choose a hash function and hash the keys into the table

  • Deal with collisions

  • Perform lookups on said table


Have you tried customizing your object to work with the built in dict type? It IS a hash table. To customize hashing, all you need to do is make sure your key objects are Hashable:

class Foo(object)
    def __hash__(self)
         #return good (int) hash for a Foo

    def __eq__(self, other)
         #return true if self == other

    def __ne__(self, other)
         #return true if self != other

Now Foo can be a key for a dict

d = {Foo(): "value1", Foo(): "value2"}