python中是否有使用字典数据结构进行编码和解码的更简单的编码

问题描述 投票:1回答:2

这里的概念是使用字典数据结构进行编码和解码。谁能帮助我做得更好,更简单?或是否有任何内置功能可以使用字典数据结构执行编码和解码?

 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)))
python list dictionary-comprehension
2个回答
2
投票

您可以将str.translatestr.maketrans结合使用:

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'}

qwerty_decrypt = {value: key for key, value in qwerty_encrypt.items()}
assert len(qwerty_decrypt) == len(qwerty_encrypt)

table_encrypt = str.maketrans(qwerty_encrypt)
table_decrypt = str.maketrans(qwerty_decrypt)

msg = 'Hello Stack Overflow'.lower()

print('After encryption:', msg.translate(table_encrypt))
print('After decryption:',
      msg.translate(table_encrypt).translate(table_decrypt))

0
投票

尽管@myrmica代码很简单易懂,

但是这是我的尝试,但清单较少。我希望这是您所期望的。

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()
print (msg)
def enigma_encrypt(msg):
    enc=[]
    for i in msg:
        if i!=' ':
            enc.append(qwerty_encrypt[i])
        elif i==' ':
            enc.append(" ")
    return "".join(enc)

def enigma_decrypt(msg):
    ms=enigma_encrypt(msg)
    dec=[]
    for j in ms:
        for u,v in qwerty_encrypt.items():
            if j!=' ' and v==j:
                dec.append(u)
            elif j==' ':
                dec.append(" ")
                break
    return "".join(dec)

print('After encrytion:',enigma_encrypt(msg)+"\n"+'After decrytion:',enigma_decrypt(msg))
Output:

After encryption: itssg lzqea gctkysgv
After decryption: hello stack overflow

© www.soinside.com 2019 - 2024. All rights reserved.