我正在尝试将文本作为输入,计算字符串中每个字母的出现次数,例如在“hello”中h = 1,e = 1,l = 2,o = 1。然后用T替换最高出现的字母,用T替换第二高的字母,依此类推(http://www.counton.org/explorer/codebreaking/frequency-analysis.php) 所以我试图在python 3中这样做,到目前为止我做了一个代码,将文本作为输入并计算每个字母的出现次数,但我的问题在于替换部分。任何人都可以帮助我这样做,如何根据该网站替换发生率最高的字母?
这是我的代码(对不起):
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()
答案 0 :(得分:0)
以下是一个例子:
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'
答案 1 :(得分:0)
不要将函数调用用作默认值
def break_cipher(OriginalText):
不要在CamelCase中命名变量,也不要用大写字母开头(这是类的惯例)
def break_cipher(original_text)
使用dict代替这些变量:
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
然后你只需要从那里重新创建文本
代码可以随身携带