Created
November 14, 2012 15:39
-
-
Save aezell/4072834 to your computer and use it in GitHub Desktop.
Dictionary comprehension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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