I’m working Huffman coding of any .txt file, so first I need to analyse this text file. I need to read it, then analyse.
I need “exit” like table:
letter | frequency(how many times same latter repeated) | Huffman code(this will come later)
I started with:
f = open('test.txt', 'r') #open test.tx
for lines in f:
print lines #to ensure if all work...
How can I order reading characters from file in alphabetic order:
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
???Many thanks
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))
But compiler return me an “error”
enter code here
I need this alphabetic order in for loop, not at end at frequencies…
To get your table of frequencies, I would use a
defaultdict. This will only iterate over the data once.