Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created February 1, 2011 15:33
Show Gist options
  • Select an option

  • Save dustingetz/806021 to your computer and use it in GitHub Desktop.

Select an option

Save dustingetz/806021 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def sorter_second_desc(x,y):
"""for sorting list of pairs by the second element, descending.
e.g. count"""
return -1 * cmp(x[1], y[1])
def main():
sentence='the quick brown FOX JumpeD Over the lazy dog'
charCount=defaultdict(int) #int factory default is 0
for ch in sentence.lower():
charCount[ch]+=1
items = charCount.items()
items.sort(sorter_second_desc)
print items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment