How do i convert a list of numbers into their corresponding chr()

旧巷老猫 提交于 2019-12-11 08:23:37

问题


c = list(range(97, 121))

if i print this it will give [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]

each of these numbers chr() string is just the alphabet but how do i convert this list to the alphabet when i print c

c = list(range(chr(97),chr(121)))

gives an error. so not really sure how to convert them all at once rather than doing them individually

thanks


回答1:


hivert's solution is really good if you want to convert a range of numbers into characters, but if you have a pre-existing list of integers that you want to convert into characters, you could adapt the solution like this:

intList = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106]
charList = [chr( intList[i] ) for i in range( 0, len( intList ) )]



回答2:


You should use a list comprehension

c = [chr(i) for i in range(97, 121)]



回答3:


intlist = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106]

charlist = [chr(x) for x in intlist]



来源:https://stackoverflow.com/questions/22227631/how-do-i-convert-a-list-of-numbers-into-their-corresponding-chr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!