📜  在python代码示例中将字符或字符串编码为整数

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

代码示例1
note:text contains your string or already read textfile 

import numpy as np
text = 'hello'
vocab=sorted(set(text))
char_to_ind = {char:ind for ind,char in enumerate(vocab)}
encoded_text = np.array([char_to_ind[c]for c in text])

your string has been encoded