4 python encryption cryptography
我正在尝试创建一个工具,在某种类型的密文中查找字母的频率.让我们假设它全是小写的az没有数字.编码的消息位于txt文件中
我正在尝试构建一个脚本来帮助破解替换或可能转换密码.
代码到目前为止:
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]
Run Code Online (Sandbox Code Playgroud)
到目前为止所做的只是显示一封信出现的次数.如何打印此文件中找到的所有字母的频率.
ber*_*nie 17
import collections
d = collections.defaultdict(int)
for c in 'test':
d[c] += 1
print d # defaultdict(<type 'int'>, {'s': 1, 'e': 1, 't': 2})
Run Code Online (Sandbox Code Playgroud)
从文件:
myfile = open('test.txt')
for line in myfile:
line = line.rstrip('\n')
for c in line:
d[c] += 1
Run Code Online (Sandbox Code Playgroud)
对于作为defaultdict容器的天才,我们必须表示感谢和赞扬.否则我们都会做这样愚蠢的事情:
s = "andnowforsomethingcompletelydifferent"
d = {}
for letter in s:
if letter not in d:
d[letter] = 1
else:
d[letter] += 1
Run Code Online (Sandbox Code Playgroud)
Vee*_*rac 10
现代方式:
from collections import Counter
string = "ihavesometextbutidontmindsharing"
Counter(string)
#>>> Counter({'i': 4, 't': 4, 'e': 3, 'n': 3, 's': 2, 'h': 2, 'm': 2, 'o': 2, 'a': 2, 'd': 2, 'x': 1, 'r': 1, 'u': 1, 'b': 1, 'v': 1, 'g': 1})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13865 次 |
最近记录: |