Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A shorthand way to get a unique integer mapper in Python
# here is a tidy way to get a hashable object (such as a word) to map to a unique int in python
from collections import defaultdict
mapper = defaultdict(lambda: len(mapper))
mapper["hello"] # 0
mapper["world"] # 1
mapper["and"] # 2
mapper["hello"] # 0
mapper["alex"] # 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.