Skip to content

Instantly share code, notes, and snippets.

@albertyw
Created August 29, 2013 01:41
Show Gist options
  • Select an option

  • Save albertyw/6373408 to your computer and use it in GitHub Desktop.

Select an option

Save albertyw/6373408 to your computer and use it in GitHub Desktop.
Most Common Letters
"""
Sort the letters of the alphabet by their frequency in the 1000 most common words
"""
# Read text file
WORDS_LIST_FILE = "common-words-list.txt"
file_handle = open(WORDS_LIST_FILE, 'r')
text = file_handle.read()
file_handle.close()
text = text.lower().replace("\n",'')
# Initialize frequency table of letters
alphabet_ascii = range(97,123)
alphabet = [chr(ascii) for ascii in alphabet_ascii]
alphabet_frequency = {}
for letter in alphabet:
alphabet_frequency[letter] = 0
# Count frequencies
for letter in text:
alphabet_frequency[letter] += 1
# Sort frequencies
alphabet = []
for letter, frequency in alphabet_frequency.items():
alphabet.append( (letter, frequency) )
alphabet.sort(key=lambda x: -x[1])
# Print nicely
for letter, frequency in alphabet:
print letter, frequency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment