用于在Python中进行序列建模的int一致的char

我正在实现RNN模型,为此我需要根据字典将所有字符转换为整数,如下所示。

charset={'!',
 '#',
 '(',
 ')',
 '+',
 '-',
 '/',
 '1',
 '2',
 '3',
 '4',
 '=',
 'B',
 'C',
 'E',
 'F',
 'H',
 'I',
 'N',
 'O',
 'P',
 'S',
 '[',
 '\\',
 ']',
 'l',
 'r'}

I use the following method to convert charset into integers based on a dictionary as follows.

char_to_int = dict((c,i) for i,c in enumerate(charset))

这给了我以下输出。

{'2':0,'F':1,'-':2,'O':3,'1':4,'E':5,'4':6,'!':   7,'H':8,'S':9,'/':10,'\':11,'#':12,'l':13,'=':   14,'P':15,'C':16,'+':17,'r':18,'B':19,'(':20,')':   21,']':22,'3':23,'[':24,'I':25,'N':26}

现在,我关闭Jupyternotebook并再次加载数据,然后再次运行上述两行代码。这次映射不同,如下所示。

{']':0,'\':1,'(':2,'=':3,'!':4,'F':5,'3':6,'S':   7,'4':8,'N':9,'+':10,'l':11,'H':12,'E':13,'C':14,   'I':15,'-':16,'B':17,')':18,'P':19,'[':20,'r':21,   '1':22,'/':23,'2':24,'#':25,'O':26}

How can I get consistent char_to_int every time I run it again.

评论
  • 史安莲
    史安莲 回复

    set objects in Python do not have order. If you make charset a list instead of a set, then your result will be consistent between runs.