这行代码做了什么?
char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }
ch:i
是什么意思?
最佳答案
这是@han solo 中提到的字典理解
最终产品是字典
它会对你的 chars
进行排序,按升序为它们附加一个数字,然后使用每个字符作为该数值的键
这是一个例子:
chars = ['d', 'a', 'b']
排序(字符)
=> ['a', 'b', 'd']
enumerate(sorted(chars))
=> 展开到 [(0, 'a'), (1, 'b'), (2, 'd')] 中的生成器对象
char_to_ix = {'a': 0, 'b': 1, 'd': 2}
关于python - "char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }"是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750750/