Created
March 26, 2013 11:08
-
-
Save timtan/5244624 to your computer and use it in GitHub Desktop.
counter with try exception style
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
__author__ = 'tim' | |
fruits = [ | |
'apple', 'pineapple', | |
'apple', 'pineapple', | |
'apple', 'pineapple', | |
] | |
counter = {} | |
for fruit in fruits: | |
try: | |
counter[fruit] += 1 | |
except KeyError: | |
counter[fruit] = 1 | |
print counter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment