我尝试将文本作为输入,计算字符串中每个字母出现的次数,例如"hello“h =1,e =1,l =2,o =1。然后将出现频率最高的字母替换为E,第二高的字母替换为T,依此类推(http://www.counton.org/explorer/codebreaking/frequency-analysis.php)所以我在Python3中尝试这样做,到目前为止,我做了一个代码,将文本作为输入,并计算每个字母表出现的次数,但我的问题是在替换部分。有人能帮我做到这一点吗?根据该网站,我如何将出现频率最高的字母表替换为出现频率最高的字母表?
下面是我的代码(很抱歉):
def break_cipher(OriginalText = input()):
a=0
b=0
c=0
d=0
e=0
f=0
g=0
h=0
i=0
j=0
k=0
l=0
m=0
n=0
o=0
p=0
q=0
r=0
s=0
t=0
u=0
v=0
w=0
x=0
y=0
z=0
for charr in OriginalText:
if charr == 'a' :
a +=1
if charr == 'b' :
b +=1
if charr == 'c' :
c +=1
if charr == 'd' :
d +=1
if charr == 'e' :
e +=1
if charr == 'f' :
f +=1
if charr == 'g' :
g +=1
if charr == 'h' :
h +=1
if charr == 'i' :
i +=1
if charr == 'j' :
j +=1
if charr == 'k' :
k +=1
if charr == 'l' :
l +=1
if charr == 'm' :
m +=1
if charr == 'n' :
n +=1
if charr == 'o' :
o +=1
if charr == 'p' :
p +=1
if charr == 'q' :
q +=1
if charr == 'r' :
r +=1
if charr == 's' :
s +=1
if charr == 't' :
t +=1
if charr == 'u' :
u +=1
if charr == 'v' :
v +=1
if charr == 'w' :
w +=1
if charr == 'x' :
x +=1
if charr == 'y' :
y +=1
if charr == 'z' :
z +=1
mylist = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v]
mylist.sort()
print(mylist)
print(mylist[-1])
str(OriginalText)
print("a = " + str(a))
print("b = " +str(b))
print("c = " +str(c))
print("d = " +str(d))
print("e = " +str(e))
print("f = " +str(f))
print("g = " +str(g))
print("h = " +str(h))
print("i = " +str(i))
print("j = " +str(j))
print("k = " +str(k))
print("l = " +str(l))
print("m = " +str(m))
print("n = " +str(n))
print("o = " +str(o))
print("p = " +str(p))
print("q = " +str(q))
print("r = " +str(r))
print("s = " +str(s))
print("t = " +str(t))
print("u = " +str(u))
print("v = " +str(v))
print("w = " +str(w))
print("x = " +str(x))
print("y = " +str(y))
print("z = " +str(z))break_cipher()
发布于 2017-11-14 21:11:46
下面是一个示例:
from collections import Counter
def break_cipher(text):
letters = sorted(Counter(text).items(), key=lambda x: x[1], reverse=True)
text = text.replace(letters[0][0],'E').replace(letters[1][0],'T')
return text
break_cipher('hello')返回:
'TeEEo'这是因为Counter(text)等于:
Counter({'e': 1, 'h': 1, 'l': 2, 'o': 1})变量letters是一个列表,其中排序如下:
[('l', 2), ('h', 1), ('e', 1), ('o', 1)]'l'变为'E','h'变为'T'
发布于 2017-11-14 21:16:31
不要使用函数调用作为默认值,因为这意味着您在定义函数时调用input(),而不是在调用它时。这个input()将被调用一次,而且只会被调用一次,这种情况会在您导入模块时发生,而不是在您调用函数时。
def break_cipher(OriginalText):不要在CamelCase中命名变量,也不要以大写字母开头(这是类的惯例)
def break_cipher(original_text)使用字典而不是这些变量:
count_dict = {}你的任务是迭代字符串,使用一个循环:
for character in original_text:
if character not in count_dict:
count_dict[character] = 0
count_dict += 1在循环的末尾,你会得到类似这样的东西:
{
'a': 5,
'e': 7
}等等。
您还可以使用defaultdict来进一步简化代码
from collections import defaultdict
count_dict = defaultdict(int) # Note that new int's are 0
for character in original_text:
count_dict[character] += 1有许多可能的子实用程序和改进:
a ==是a吗?在这种情况下,您需要执行以下操作:
count_dict[character.lower()] += 1你打算只计算英文字母表字符吗?在这种情况下,您需要:
for character in [charr for charr in count_dict if 'a' < charr.lower() < 'z'](关于这一点,参见list comprehensions )
对于更换部件:
frequencies = count_dict.keys() # This gives you a list of all the letters in the text
frequencies.sort(key=count_dict).reverse() # This will sort them by frequency然后,您只需要从那里重新创建文本
愿代码与你同在
https://stackoverflow.com/questions/47284922
复制相似问题