返回列表中字符串的 ASCII 代码



我正在尝试编写接受字符串列表的代码,并返回一个字典,其中包含字符串作为键和相应的字符代码列表作为值。我正在使用字典理解,这就是我所拥有的。

def get_code(words): 
ascii = {} 
ascii = [[ord(ch) for ch in word] for word in words]
return ascii

测试后

words = ['yes','no'], i get [[121, 101, 115], [110, 111]]  as the output. 
This {'yes': [121, 101, 115], 'no': [110, 111]} is what i want to get. 

请指教。

尝试字典理解:

def get_code(words): 
ascii = {word: [ord(ch) for ch in word] for word in words}
return ascii

当然,您不能期望从列表理解中获得字典:)

最新更新