Answer the question
In order to leave comments, you need to log in
Arithmetic coding implementation in python?
Sample coding in Excel
I already have a frequency count
import collections
co = collections.Counter()
file_txt = open("test.txt","r", encoding='utf-8')
for line in file_txt:
co.update(line.lower())
total, lo = sum(co.values()), 0
for k, v in co.most_common():
hi = lo + v
print('%f\t%c\t%f' % (lo / total, k, hi / total))
lo = hi
0.000000 ш 0.272727
0.272727 у 0.545455
0.545455 м 0.727273
0.727273 р 0.818182
0.818182 0.909091
0.909091 о 1.000000
Answer the question
In order to leave comments, you need to log in
json is fast enough and human readable. You only read it once, and not at each iteration. So everything is ok.
Read file letter by letter:
# Открыть файл для чтения в текстовом формате с кодировкой UTF-8
with open(ФАЙЛ, mode='tr', encoding='utf8') as f:
# Повторять вечно
while True:
# считать один символ
c = f.read(1)
# если ничего не считано, выходим из повторения
if not c:
break
# обработка символа с
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question