在python3中破译文本

2024-03-28 19:32:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图以文本作为输入,计算字符串中每个字母出现的次数,例如在“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))

破译密码()


Tags: 字符串文本http密码helloif字母次数
2条回答

举个例子:

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({'e': 1, 'h': 1, 'l': 2, 'o': 1})

变量letters是一个排序如下的列表:

[('l', 2), ('h', 1), ('e', 1), ('o', 1)]

'l'变成'E''h'变成'T'

不要将函数调用用作默认值,因为这意味着您在定义函数时调用的是input(),而不是在调用函数时。这个input()将被调用一次,而且只调用一次,并且这将在导入模块时发生,而不是在调用函数时发生。你知道吗

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

然后你只需要重新创建文本

愿密码与你同在

相关问题 更多 >