不知道如何解码我刚刚用 Python 编写的编码文本
python 509
原文标题 :Don’t know how to decode encoded text I just wropte in Python
我有这段代码可以将用户输入的任何文本加密为数字,如下所示:
代码:
text = input()
def encrypt(t):
chars = list(text)
allowed_characters = list(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?!")
for char in chars:
for i in allowed_characters:
if char == i:
chars[chars.index(char)] = allowed_characters.index(i)
return chars
print(encrypt(text))
输出:(我输入狗作为要加密的文本)
[4, 15, 7, 19]
回复
我来回复-
hochae 评论
您可以使用 dict 对字符串进行编码和解码。在这种情况下,它会比 list 更简单。请检查此代码。
ac = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?!" dict_e = { k:v for v, k in enumerate(ac)} dict_d = { k:v for k, v in enumerate(ac)} def encrypt(t): return [dict_e[x] for x in t] def decode(t): return [dict_d[x] for x in t] text = input() print(encrypt(text)) print(''.join(decode(encrypt(text))))
2年前