确定密文的字母频率 [英] Determining Letter Frequency Of Cipher Text

查看:390
本文介绍了确定密文的字母频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个工具,找到某些类型的密文中的字母的频率。
假设它都是小写的a-z没有数字。编码的邮件在一个txt文件中



我试图构建一个脚本来帮助破解替换或可能的转置密码。



到目前为止的代码:

  cipher = open('cipher.txt','U')。 ()
cipherfilter = cipher.lower()
cipherletters = list(cipherfilter)

alpha = list('abcdefghijklmnopqrstuvwxyz')
occurrences = {}
在字母中的字母:
occurrences [letter] = cipherfilter.count(letter)
for letter in occurrences:
打印字母,出现[letter]

到目前为止,它只显示一封信出现的次数。
如何打印此文件中找到的所有字母的频率。

解决方案

  import collections 

d = collections.defaultdict(int)
for'test':
d [c] + = 1

print d #defaultdict(< type'int'>,{'s':1,'e':1,'t':2})

从文件:

  myfile = open('test.txt')
在myfile中的行:
line = line.rstrip('\\\
')
for c in line:
d [c] + = 1

对于 defaultdict 容器,我们必须给予感谢和赞美。否则,我们都会做这样的事情:

  s =andnowforsomethingcompletelydifferent
d = {}
表示s中的字母:
如果字母不在d:
d [letter] = 1
else:
d [letter] + = 1


I am trying to make a tool that finds the frequencies of letters in some type of cipher text. Lets suppose it is all lowercase a-z no numbers. The encoded message is in a txt file

I am trying to build a script to help in cracking of substitution or possibly transposition ciphers.

Code so far:

cipher = open('cipher.txt','U').read()
cipherfilter = cipher.lower()
cipherletters = list(cipherfilter)

alpha = list('abcdefghijklmnopqrstuvwxyz')
occurrences = {} 
for letter in alpha:
    occurrences[letter] = cipherfilter.count(letter)
for letter in occurrences:
    print letter, occurrences[letter]

All it does so far is show how many times a letter appears. How would I print the frequency of all letters found in this file.

解决方案

import collections

d = collections.defaultdict(int)
for c in 'test':
    d[c] += 1

print d # defaultdict(<type 'int'>, {'s': 1, 'e': 1, 't': 2})

From a file:

myfile = open('test.txt')
for line in myfile:
    line = line.rstrip('\n')
    for c in line:
        d[c] += 1

For the genius that is the defaultdict container, we must give thanks and praise. Otherwise we'd all be doing something silly like this:

s = "andnowforsomethingcompletelydifferent"
d = {}
for letter in s:
    if letter not in d:
        d[letter] = 1
    else:
        d[letter] += 1

这篇关于确定密文的字母频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆