📜  在 python 中将 int 编码为 chr,反之亦然 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:44.818000             🧑  作者: Mango

代码示例1
# encode the text and map each character to an integer and vice versa

# we create two dictionaries:
# 1. int2char, which maps integers to characters
# 2. char2int, which maps characters to unique integers
chars = tuple(set(text))
int2char = {ind:char for ind,char in enumerate(chars)}
char2int = {char:ind for ind,char in enumerate(chars)}

# encode the text
encoded = np.array([char2int[ch] for ch in text])