我正在爲學校創建一個簡單的加密項目。我正在與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