[英]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
? ch:i
是什么意思?
It is dict comprehension. 这是字典理解。 ch - it is key in dictionary, i - value for that key. ch-它是字典中的键,i-该键的值。
Dictionary syntax is 字典语法为
dict = {
key1: value1,
key2: value2
}
With your code you will generate key: value
pairs from enumerated chars. 使用您的代码,您将通过枚举的字符生成key: value
对。 Key would be an element of sorted list of chars. 键将是字符排序列表的元素。 Value - index of that element 值-该元素的索引
this is a dict comprehension as mentioned in by @han solo 这是@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
排序,将它们按升序附加一个数字,然后将每个字符用作该数值的键,这是一个示例:
chars = ['d', 'a', 'b']
sorted(chars)
=> ['a', 'b', 'd']
sorted(chars)
=> ['a', 'b', 'd']
enumerate(sorted(chars))
=> a generator object that unrolls into [(0, 'a'), (1, 'b'), (2, 'd')]
enumerate(sorted(chars))
=>一个生成器对象,展开为[(0, 'a'), (1, 'b'), (2, 'd')]
char_to_ix = {'a': 0, 'b': 1, 'd': 2}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.