Created
August 17, 2013 00:50
-
-
Save alexbowe/6254690 to your computer and use it in GitHub Desktop.
A shorthand way to get a unique integer mapper in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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