帮助修复反向方法...(文本到数字,然后反向,数字到文本) 以下是未经编辑的完整代码。
chars=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","t","s","u","v","w","x","y","z","!","@","#","$","%","^","&","*","(",")","<",">","?","/","[","]","{","}","-","_","=","+"," "];
numbs1=["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49"];
rechars1=dict(zip(chars,numbs1));
rechars2=dict(zip(numbs1,chars)); # ??
stringa=raw_input(""); #Letters and symbols only-->encrypt
stringb=''.join(rechars1.get(c,c) for c in stringa);
stringc=''.join(rechars2.get(c,c) for c in stringb); # ??
print "Plain: ",stringa;
print "Encoded: ",stringb;
print "Unencoded: ",stringc; #same as stringb??
答案 0 :(得分:2)
如果您知道每个输入字符都已翻译(不包含数字或其他字符),那么您可以按如下方式对其进行反转:
decipher = dict(zip(numbs1,chars))
stringc = ''.join(decipher[stringb[i:i+2]] for i in range(0, len(stringb), 2));
print "Decoded: ", stringc
然而,如果没有翻译单个输入字符,那么像这里一样的字符对将不起作用(因为每对字符在原始字符中不会排成一行)
答案 1 :(得分:1)
路障是如何将'20051920'等字符串分成两位数的列表。如果你克服了这个障碍,你可以使用 rechars2 将其翻译回来。
我的解决方案使用正则表达式:
import re
stringb = '20051920'
print re.findall(r'\d\d', stringb) # ['20', '05', '19', '20']