RosettaCodeData/Task/Entropy/Python/entropy-2.py

9 lines
218 B
Python
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
from math import log2
from collections import Counter
def entropy(s):
p, lns = Counter(s), float(len(s))
return log2(lns) - sum(count * log2(count) for count in p.values()) / lns
print(entropy("1223334444"))