[英]Returning ASCII codes for strings in a list
我正在嘗試編寫接受字符串列表並返回包含字符串作為鍵和相應字符代碼列表作為值的字典的代碼。 我正在使用字典理解,這就是我所擁有的。
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
當然,您不能期望從列表理解中獲得字典:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.