Skip to content

Instantly share code, notes, and snippets.

@alexbowe
Created August 17, 2013 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbowe/6254690 to your computer and use it in GitHub Desktop.
Save alexbowe/6254690 to your computer and use it in GitHub Desktop.
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