encoding character or string to integer in python

Solutions on MaxInterview for encoding character or string to integer in python by the best coders in the world

showing results for - "encoding character or string to integer in python"
Enrico
13 Jun 2019
1note:text contains your string or already read textfile 
2
3import numpy as np
4text = 'hello'
5vocab=sorted(set(text))
6char_to_ind = {char:ind for ind,char in enumerate(vocab)}
7encoded_text = np.array([char_to_ind[c]for c in text])
8
9your string has been encoded