3

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:

  • Create the table
  • Choose a hash function and hash the keys into the table
  • Deal with collisions
  • Perform lookups on said table

1 Answer 1

3

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"} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.