簡體   English   中英

如何將數字列表轉換為字母列表,Python

[英]How to convert a list of numbers to a list of letters, Python

python的新手,我正在嘗試編碼加密的消息

字母已被重寫如下:

cipher = [ "q", "a", "z", "w", "s", "x", "e", "d", "c", "r", "f", "v", "t", "g", "b", "y", "h", "n", "u", "j", "m", "i", "k", "o", "l", "p"]

idx = ord ("d") - ord ("a")

print(cipher[3])

輸出:w

input = str(input('Write Text: ')) #hello world used here
input = input.lower()
output = []
for character in input:
    number = ord(character) - 97
    output.append(number)
print(output)

輸出:

[7,4,11,11,14,-65,22,14,17,11,3]

不確定如何對應這兩個...

我試過了:

print(cipher[output])

返回TypeError:列表索引必須是整數或切片,而不是list

提前致謝

我認為您想要類似的東西:

deciphered = [cipher[i] if i > 0 else ' ' for i in output]
print(''.join(deciphered))

請注意i>0位在那里處理空間,這導致number -65 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM