- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作的词典将字母替换为短划线 (-) 和两个数字。在我的字典里,“t”=“-21”。当我从字典中调用“t”时,我得到“-21”。但是当我调用 replace_all(text, dic)
时,输出是“-2--5-63-5-63”知道出了什么问题吗?
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ldic3 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
>>> def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
#Example A
>>> var = "t"
>>> var2 = replace_all(var, ldic3)
>>> var2
'-2--5-63-5-63'
>>> ldic3["t"]
'-21'
#Example B
>>> var = "a"
>>> var2 = replace_all(var, ldic3)
>>> var2
'-02'
>>>
快速编辑:
这是为了让文本不可读,通过一遍又一遍地改变它。在不破坏我的程序的情况下,我不能将我的字符串变成它自己的字典。这是为了一次更改整个段落中的字符。
此外,“replace_all(text, dic)”的代码是我在网上找到的,我不确定它是如何工作的。
这是我的程序:
from Tkinter import *
import time
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
passchar = {"a":"0","b":"1","c":"2","d":"3","e":"0","f":"1","g":"2","h":"3","i":"0","j":"1","k":"2","l":"3","m":"0","n":"1","o":"2","p":"3","q":"0","r":"1","s":"2","t":"3","u":"0","v":"1","w":"2","x":"3","y":"0","z":"1"," ":""}
ldic0 = {"a":"-82","b":"-93","c":"-11","d":"-91","e":"-88","f":"-27","g":"-01","h":"-17","i":"-72","j":"-90","k":"-99","l":"-42","m":"-21","n":"-12","o":"-81","p":"-28","q":"-73","r":"-96","s":"-92","t":"-87","u":"-86","v":"-62","w":"-00","x":"-31","y":"-69","z":"-77","A":"-02","B":"-09","C":"-43","D":"-10","E":"-46","F":"-78","G":"-13","H":"-24","I":"-35","J":"-46","K":"-05","L":"-83","M":"-96","N":"-70","O":"-98","P":"-48","Q":"-29","R":"-32","S":"-52","T":"-94","U":"-53","V":"-66","W":"-44","X":"-51","Y":"-71","Z":"-80","0":"-97","1":"-39","2":"-26","3":"-54","4":"-22","5":"-07","6":"-40","7":"-30","8":"-50","9":"-60"}
ldic1 = {"a":"-01","b":"-02","c":"-03","d":"-04","e":"-05","f":"-06","g":"-07","h":"-08","i":"-09","j":"-10","k":"-11","l":"-12","m":"-13","n":"-14","o":"-15","p":"-16","q":"-17","r":"-18","s":"-19","t":"-20","u":"-21","v":"-22","w":"-23","x":"-24","y":"-25","z":"-26","A":"-27","B":"-28","C":"-29","D":"-30","E":"-31","F":"-32","G":"-33","H":"-34","I":"-35","J":"-36","K":"-37","L":"-38","M":"-39","N":"-40","O":"-41","P":"-42","Q":"-43","R":"-44","S":"-45","T":"-46","U":"-47","V":"-48","W":"-49","X":"-50","y":"-51","Z":"-52","0":"-53","1":"-54","2":"-55","3":"-56","4":"-57","5":"-58","6":"-59","7":"-60","8":"-61","9":"-62"}
ldic2 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
ldic3 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
#ldic3 = {"a":"-62","b":"-61","c":"-60","d":"-59","e":"-58","f":"-57","g":"-56","h":"-55","i":"-54","j":"-53","k":"-52","l":"-51","m":"-50","n":"-49","o":"-48","p":"-47","q":"-46","r":"-45","s":"-44","t":"-43","u":"-42","v":"-41","w":"-40","x":"-39","y":"-38","z":"-37","A":"-36","B":"-35","C":"-34","D":"-33","E":"-32","F":"-31","G":"-30","H":"-29","I":"-28","J":"-27","K":"-26","L":"-25","M":"-24","N":"-23","O":"-22","P":"-21","Q":"-20","R":"-19","S":"-18","T":"-17","U":"-16","V":"-15","W":"-14","X":"-13","Y":"-12","Z":"-10","0":"-09","1":"-08","2":"-07","3":"-06","4","-05","5":"-04","6":"-03","7":"-02","8":"-01","9":"-00"}
ndic0 = {"0":"3","1":"4","2":"1","3":"7","4":"9","5":"8","6":"2","7":"5","8":"6","9":"0"}
ndic1 = {"0":"1","1":"4","2":"3","3":"5","4":"0","5":"8","6":"2","7":"6","8":"9","9":"7"}
ndic2 = {"0":"2","1":"9","2":"8","3":"4","4":"7","5":"3","6":"0","7":"1","8":"6","9":"5"}
ndic3 = {"0":"4","1":"2","2":"3","3":"9","4":"0","5":"1","6":"7","7":"8","8":"6","9":"5"}
class Application(Frame):
def __init__(self, master):
#Initialize the frame
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction = Label(self, text = "ENCRYPTION KEY:")
self.instruction.grid(row = 0, column = 1, columnspan = 2, sticky = N) #sticky = direction W = West
self.key = Entry(self) #Promps for user input
self.key.grid(row = 1, column = 2, sticky = N)
#SPACER1
self.spacer1 = Label(self, text = " ")
self.spacer1.grid(row = 2, column = 0, columnspan = 2, sticky = N)
#-------------
#ENCRYPT BUTTON
self.encrypt_button = Button(self, width = 18, height = 2, text ="ENCRYPT", command = self.textboxa1)
self.encrypt_button.grid(row = 3, column = 0, sticky = N)
#CLEAR BUTTON
self.clear_button = Button(self, text ="CLEAR", width = 18, command = self.clearboxes)
self.clear_button.grid(row = 3, column = 2, sticky = N)
#DECRYPT BUTTON
self.decrypt_button = Button(self, width = 18, height = 2, text ="DECRYPT",) #command = self.textboxa2
self.decrypt_button.grid(row = 3, column = 3, sticky = N)
#Top TextBox
self.textbox1 = Text(self, width = 50, height = 15, wrap = WORD) # wrap = WORD CHAR or NONE
self.textbox1.grid(row = 4, column = 0, columnspan = 4, sticky = N)
#-------------
#SPACER2
self.spacer2 = Label(self, text = " ")
self.spacer2.grid(row = 5, column = 0, columnspan = 2, sticky = N)
#Bottom TextBox
self.textbox2 = Text(self, width = 50, height = 15, wrap = WORD)
self.textbox2.grid(row = 6, column = 0, columnspan = 4, sticky = N)
def textboxa1(self):
keyvar = self.key.get()
textvar = self.textbox1.get(0.0, END)
keyvarl = len(keyvar)
if keyvarl > 25 or keyvarl < 3:
self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, '"' + keyvar + '"' + " -ERROR PASSWORD MUST BE 3 - 25 CHARACTERS LONG")
else:
step = 0
num = 0
passcode = replace_all(keyvar, passchar)
#self.textbox2.delete(0.0, END)
#self.textbox2.insert(0.0, "start")
for i in range(keyvarl):
char = list(passcode)
dig = passcode[num]
self.textbox2.insert(0.0, "")
if dig == "0":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic0)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "0")
else:
textvar2 = replace_all(textvar2, ndic0)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "0")
elif dig == "1":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic1)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "1")
else:
textvar2 = replace_all(textvar2, ndic1)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "1")
elif dig == "2":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic2)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "2")
else:
textvar2 = replace_all(textvar2, ndic2)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "2")
else:
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic3)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "3")
else:
textvar2 = replace_all(textvar2, ndic3)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "3")
keyvar2 = keyvarl - 1
if i == keyvar2:
#self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, textvar2 + textvar)
else:
#self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, textvar2 + textvar)
self.textbox2.insert(0.0, keyvar2)
self.textbox2.insert(0.0, " of ")
self.textbox2.insert(0.0, num)
self.textbox2.insert(0.0, "Step: ")
# self.textbox2.insert(0.0, textvar2 + textvar)
num = num + 1
# def textboxa2(self):
# input1 = self.key.get()
# input2 = self.textbox1.get(0.0, END)
# self.textbox2.delete(0.0, END)
# self.textbox2.insert(0.0, input1 + input2)
def clearboxes(self):
self.textbox1.delete(0.0, END)
self.textbox2.delete(0.0, END)
root = Tk()
root.title("FirePie Demo - Message Encryptor!")
root.geometry("403x630")
app = Application(root)
root.mainloop()
最佳答案
“t”替换为“-21”
然后“1”被替换为“-55”
然后两个“5”都被“-59”替换,结果是“-2--59-59”
然后将两个“9”都替换为“-63”,得到返回的“-2--5-63-5-63”。
如果您不想替换之前替换的字母之一,请考虑一次替换一个字母:
def replace_all(text, dic):
return ''.join(dic.get(x, x) for x in text)
get
中的第二个 x
是默认值,以防 dict
没有键 x
.
关于python - 我有一个字典 Python 2.7 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979709/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!