Reputation: 1697
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.
Basically:
Upvotes: 3
Views: 217
Reputation: 65639
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"}
Upvotes: 3