Skip to content

Instantly share code, notes, and snippets.

@Nikolaj-K
Forked from pozhidaevak/letterFrequency.py
Last active July 1, 2022 15:06
Show Gist options
  • Save Nikolaj-K/9f23aa3805a4a359942d7a53a0410821 to your computer and use it in GitHub Desktop.
Save Nikolaj-K/9f23aa3805a4a359942d7a53a0410821 to your computer and use it in GitHub Desktop.
Python dict with English letter frequency
LETTER_FREQUENCIES = {'E' : 12.0,
'T' : 9.10,
'A' : 8.12,
'O' : 7.68,
'I' : 7.31,
'N' : 6.95,
'S' : 6.28,
'R' : 6.02,
'H' : 5.92,
'D' : 4.32,
'L' : 3.98,
'U' : 2.88,
'C' : 2.71,
'M' : 2.61,
'F' : 2.30,
'Y' : 2.11,
'W' : 2.09,
'G' : 2.03,
'P' : 1.82,
'B' : 1.49,
'V' : 1.11,
'K' : 0.69,
'X' : 0.17,
'Q' : 0.11,
'J' : 0.10,
'Z' : 0.07 } # Not properly normalized, sums to about 99.97
d = {k: v / sum(LETTER_FREQUENCIES.values()) for k, v in LETTER_FREQUENCIES.items()}
if __name__=="__main__":
import numpy
# Note: I assume the python version is recent enough for all list orderes computed from the dict to be aligned
p = [v / sum(d.values()) for v in d.values()]
random_letter: str = numpy.random.choice(list(d.keys()), p=p)
print(random_letter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment