encoding int to chr in python and vice versa

Solutions on MaxInterview for encoding int to chr in python and vice versa by the best coders in the world

showing results for - "encoding int to chr in python and vice versa"
Lyle
10 Feb 2016
1# encode the text and map each character to an integer and vice versa
2
3# we create two dictionaries:
4# 1. int2char, which maps integers to characters
5# 2. char2int, which maps characters to unique integers
6chars = tuple(set(text))
7int2char = {ind:char for ind,char in enumerate(chars)}
8char2int = {char:ind for ind,char in enumerate(chars)}
9
10# encode the text
11encoded = np.array([char2int[ch] for ch in text])
similar questions