【发布时间】:2019-11-12 08:58:25
【问题描述】:
这里的概念是使用字典数据结构进行编码和解码。谁能帮助我做得更好,更简单? 或者是否有任何内置函数可以使用字典数据结构执行编码和解码?
qwerty_encrypt={'a':'q','b':'w','c':'e','d':'r','e':'t','f':'y','g':'u','h':'i','i':'o','j':'p','k':'a','l':'s','m':'d','n':'f','o':'g','p':'h','q':'j','r':'k','s':'l','t':'z','u':'x','v':'c','w':'v','x':'b','y':'n','z':'m','1':'9','2':'8','3':'7','4':'6','5':'5','6':'4','7':'3','8':'2','9':'1','0':'0'}
msg=input('enter yuor msg:').lower()
#encrypt_func
def enigma_encrypt(msg):
a=list(msg.split(' '))
li=[]
for i in a:
li.append(list(i))
li2=[]
li4=[]
for i,j in enumerate(li):
li3=[]
for k,l in enumerate(j):
li3.append(qwerty_encrypt[l])
li2.append(li3)
for m in li2:
li4.append(''.join(m))
after_encrypt=(' '.join(li4))
return after_encrypt
def enigma_decrypt(msg):
a=list(msg.split(' '))
li=[]
for i in a:
li.append(list(i))
li2=[]
li4=[]
for i,j in enumerate(li):
li3=[]
for k,l in enumerate(j):
for n in qwerty_encrypt:
if qwerty_encrypt[n]==l:
li3.append(n)
li2.append(li3)
for m in li2:
li4.append(''.join(m))
after_encrypt=(' '.join(li4))
return after_encrypt
print('After encrytion:',enigma_encrypt(msg),'\nAfter decryption:',enigma_decrypt(enigma_encrypt(msg)))
【问题讨论】:
-
为什么要使用这么多列表? msg 将是一个可迭代的字符串。我也会做一个解密字典。然后几乎有两个循环输出您的解密消息和加密消息。 {" ":" "} 也是一个有效的字典
标签: python list dictionary-comprehension