我正在对任何 .txt 文件进行霍夫曼编码,所以首先我需要分析这个文本文件。我需要阅读它,然后分析。我需要像表格一样的“退出”:
信 | 频率(后面重复多少次)| 霍夫曼代码(稍后会出现)
我开始:
f = open('test.txt', 'r') #open test.tx
for lines in f:
print lines #to ensure if all work...
如何按字母顺序从文件中读取字符:
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
???非常感谢
Well I tried like this:
frequencies = collections.defaultdict(int)
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
frequencies = [(count, char) for char, count in frequencies.iteritems()]
frequencies.sort(key=operator.itemgetter(1))
但是编译器给我一个“错误”在这里输入代码
我在 for 循环中需要这个字母顺序,而不是在频率结束时......