C
C
coldunox2018-03-04 21:49:47
Python
coldunox, 2018-03-04 21:49:47

How to split the interval [0; 1] into half-intervals?

import collections

co = collections.Counter()
file_txt = open("test.txt","r", encoding='utf-8')
for line in file_txt:
    co.update(line.lower())

d = dict(co)
count = sum(d.values())

low = 0.0
high = 1.0
for key, value in d.items():
    print( low ,': ', key,':',  value*1/count)

In a text file lies - I rustle with noise.
The output looks like this so far
0.0 :  ш : 0.2727272727272727
0.0 :  у : 0.2727272727272727
0.0 :  р : 0.09090909090909091
0.0 :    : 0.09090909090909091
0.0 :  м : 0.18181818181818182
0.0 :  о : 0.09090909090909091

Should be like this
0                Ш  0,272727273 
0,272727273      У  0,545454545 
0,545454545      М  0,727272727 
0,727272727      Р  0,818181818 
0,818181818      _  0,909090909 
0,909090909      О  1

What condition must be written for everything to work? Or create a variable? That is, I have frequencies. It is known that the initial value is 0, the final value is 1. At the second step, the initial value will be the final number of the first step, and the final value of the second step is the sum of the final values ​​of the first and second.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-03-04
@coldunox

from collections import Counter

co = Counter("шуршу шумом")
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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question