Skip to content

Instantly share code, notes, and snippets.

@aezell
Created November 14, 2012 15:39
Show Gist options
  • Save aezell/4072834 to your computer and use it in GitHub Desktop.
Save aezell/4072834 to your computer and use it in GitHub Desktop.
Dictionary comprehension
# stolen from http://stackoverflow.com/a/7276556/64266
def countChar(word):
return dict((item, word.count(item)) for item in set(word))
>>> countChar('google')
{'e': 1, 'g': 2, 'o': 2, 'l': 1}
>>> countChar('apple')
{'a': 1, 'p': 2, 'e': 1, 'l': 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment