我正在为学校创建一个简单的加密项目。我正在与Python合作。python字符串到数组
目标:用户输入字符串,例如, hello world!
我得把它转换成一个数组:space=0, a=1, b=2, ... z=26, .=27, ,=28, ?=29 and !=30
我用的字典:
dict = {' ': 0, 'a': 1, 'b': 2, 'c': 3,...}
我的代码
def messageToCode(message):
xarray = [None]
length = len(message)
ctr = 0
while not ctr == length:
xarray.append = dict[message.charAt(ctr)]
ctr = ctr + 1
return xarray
这是行不通的。有什么建议么?
什么不适用于它?它会抛出一个错误吗?输出是不同的?怎么会这样? – Celeo
相关:http://stackoverflow.com/questions/8848294/how-to-get-char-from-string-by-index – jakevdp
'[dict [x] for x in message]',也许? – twalberg