Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
lfa:dictionaries [2021/09/28 13:13] pdmatei |
lfa:dictionaries [2021/09/28 13:21] (current) pdmatei |
||
---|---|---|---|
Line 13: | Line 13: | ||
Good dispersion functions: | Good dispersion functions: | ||
* If the function $math[hash] is a **good** dispersion function, then its range (co-domain) will be large, which means that we have a lot of buckets with very few pairs inside them. | * If the function $math[hash] is a **good** dispersion function, then its range (co-domain) will be large, which means that we have a lot of buckets with very few pairs inside them. | ||
- | * A very **bad** dispersion function is the identity function, which performs exactly like an array (a single bucket - with all pairs inside it) | + | * A very **bad** dispersion function is the constant function, which performs exactly like an array (a single bucket - with all pairs inside it) |
* The **best possible** dispersion function would have one bucket per pair | * The **best possible** dispersion function would have one bucket per pair | ||
Line 35: | Line 35: | ||
print(d[l]) # we use hash(l) to obtain the value assigned to l, but l was changed? | print(d[l]) # we use hash(l) to obtain the value assigned to l, but l was changed? | ||
# how would the hash function work to always compute the same bucket in which l is assigned? | # how would the hash function work to always compute the same bucket in which l is assigned? | ||
+ | </code> | ||
+ | |||
+ | Sets, as well as other datatypes who's value can change, cannot be used as keys in Python. An option is to use, if necessary, ''frozenset'', which is the immutable alternative to sets in Python. | ||
+ | |||
+ | ===== Writing your own hash function ===== | ||
+ | |||
+ | So far, we have not pointed out who the hash function actually is. For predefined datatypes, Python implements efficient hash functions. However, we can also define hash functions for objects that we create: | ||
+ | |||
+ | <code python> | ||
+ | class O: | ||
+ | def __init__(self,x): | ||
+ | self.x = x | ||
+ | # this special function is the hashing function implemented for objects of type ''O''. Now we can use them as keys in a dictionary | ||
+ | def __hash__(self): | ||
+ | return 0 # this implementation is not really efficient, but it simply illustrates the syntax for defining a hash-function of your own | ||
+ | |||
+ | d = {} | ||
+ | ob = O(45) | ||
+ | d[ob] = "hello" | ||
+ | d[O(1)] = "kitty" # the pairs O(45),"hello" and O(1),"kitty" will share the same bucket (namely 0) | ||
+ | |||
</code> | </code> | ||
+ | |||
+ | |||
+ |