python - What does "char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }" do?
问题描述
What does this line of code do?
char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }
what is the meaning of ch:i
?
解决方案
this is a dict comprehension as mentioned in by @han solo
the final product is a dict
it will sort your chars
, attach a number in ascending order to them, and then use each character as the key to that numerical value
here's an example:
chars = ['d', 'a', 'b']
sorted(chars)
=> ['a', 'b', 'd']
enumerate(sorted(chars))
=> a generator object that unrolls into [(0, 'a'), (1, 'b'), (2, 'd')]
char_to_ix = {'a': 0, 'b': 1, 'd': 2}
推荐阅读
- windows - 将 Windows Server 2019 从 1809 更新到 1903
- c# - 如何在 c# 中修复错误 {“Expected to read 4 header bytes but only received 0.”}?
- javascript - 使用 CSS 分隔单词组并强制换行
- ios - 更改运行时 ios 上的默认应用程序语言
- c++ - 按第二列对向量进行排序
- wordpress - WordPress 开发 / 将 $post obj 插入数组
- intellij-idea - 我如何从 psiFile 中获取所有类(和内部类)?(智能插件)
- python-3.x - 如何在班级之间切换
- ios - 应用程序加载器无法上传您的包颤振应用程序
- perl - 使用 WWW::Mechanize 为 application/x-www-form-urlencoded 发布消息