I am trying to implement a LHS pattern match with a RHS action code in Python How can I get a fast hashtable match. Is this possible in Python? I need to fast match features in terms of x,y,c where x and y are coordinates and c is the color at index(x,y) of a 2d array.
-
3Hashtable is called [`dict`](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) in Python. – Amadan Apr 07 '19 at 23:22
-
1And check this answer to see which types can be hashed: https://stackoverflow.com/a/44880799/937153 – Unni Apr 07 '19 at 23:23
2 Answers
An hashmap is a dictionary in python.
There are several ways to create dictionaries, here's 2:
d = dict(k=v)
or
d = {k:v}
To get the value of a key:
k = d.get("k")
or
k = d[k]
To set the value of a key:
d[k] = "ok"
Notes:
In Python, dictionaries (or “dicts”, for short) are a central data structure:
Dicts store an arbitrary number of objects, each identified by a unique dictionary key. Dictionaries are often also called maps, hashmaps, lookup tables, or associative arrays. They allow the efficient lookup, insertion, and deletion of any object associated with a given key.
Resources:

- 94,083
- 31
- 258
- 268
-
A dictionary is a hashmap, not a hashtable. You can map values to hashable keys, but the keys are still just a set, not a table. – sleblanc Apr 07 '19 at 23:55
The native type dict
is a hashmap, not a hashtable, therefore you can only values to a key.
You can however simulate a hashtable by using (x, y)
tuples as keys:
d = {}
d[(1,0)] = True
d[(1,1)] = False
This works because the tuple
type in Python is hashable, meaning that as long as the values that it wraps are hashable, it can convert the value to a key.
Otherwise, you could extend the dict type to provide additional methods, letting you access values in a Java- or C-style 2D array:
d[1][0] = True
d[1][1] = False

- 3,821
- 1
- 34
- 42