Skip to content

Instantly share code, notes, and snippets.

@npinto
Created September 4, 2012 21:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npinto/3626866 to your computer and use it in GitHub Desktop.
Save npinto/3626866 to your computer and use it in GitHub Desktop.
String labels to sorted 0-based integers.
In [12]: l = ['a', 'a', 'c', 'c', 'c', 'b']
In [13]: u = np.unique(l)
In [14]: u
Out[14]:
array(['a', 'b', 'c'],
dtype='|S1')
In [15]: np.searchsorted(u, l)
Out[15]: array([0, 0, 2, 2, 2, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment