Skip to content

Instantly share code, notes, and snippets.

@macro1
Last active November 9, 2015 08:43
Show Gist options
  • Save macro1/9b364612ee3907df4179 to your computer and use it in GitHub Desktop.
Save macro1/9b364612ee3907df4179 to your computer and use it in GitHub Desktop.
Python 1.3 (Apr 13 1996) [MSC 32 bit (Intel)]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> colors = ["brown", "red", "green", "yellow", "yellow", "brown", "brown", "black"]
>>> color_counts = {}
>>> for c in colors:
... if color_counts.has_key(c):
... color_counts[c] = color_counts[c] + 1
... else:
... color_counts[c] = 1
...
>>> color_counts
{'green': 1, 'black': 1, 'brown': 3, 'yellow': 2, 'red': 1}
>>> color_counts = {}
>>> for c in colors:
... if not color_counts.has_key(c):
... color_counts[c] = 0
... color_counts[c] = color_counts[c] + 1
...
>>> color_counts
{'red': 1, 'yellow': 2, 'brown': 3, 'black': 1, 'green': 1}
>>> color_counts = {}
>>> for c in colors:
... try:
... color_counts[c] = color_counts[c] + 1
... except KeyError:
... color_counts[c] = 1
...
>>> color_counts
{'green': 1, 'black': 1, 'brown': 3, 'yellow': 2, 'red': 1}
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> colors = ["Brown", "Red", "Green", "White", "Yellow", "Yellow", "Brown", "Brown", "Black"]
>>> color_counts = {}
>>> for c in colors:
... color_counts[c] = color_counts.get(c, 0) + 1
...
>>> color_counts
{'Red': 1, 'Black': 1, 'Green': 1, 'Yellow': 2, 'White': 1, 'Brown': 3}
Python 2.0.1 (#1, Nov 6 2015, 17:35:43)
[GCC 5.2.1 20151028] on linux4
Type "copyright", "credits" or "license" for more information.
>>> colors = ["Brown", "Red", "Green", "White", "Yellow", "Yellow", "Brown", "Brown", "Black"]
>>> color_counts = {}
>>> for c in colors:
... color_counts.setdefault(c, 0)
... color_counts[c] += 1
...
0
0
0
0
0
1
1
2
0
>>> color_counts
{'Red': 1, 'Yellow': 2, 'White': 1, 'Green': 1, 'Black': 1, 'Brown': 3}
Python 2.3.7 (#2, Jan 24 2014, 18:40:40)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> colors = ["brown", "red", "green", "yellow", "yellow", "brown", "brown", "black"]
>>> color_counts = dict.fromkeys(colors, 0)
>>> for c in colors:
... color_counts[c] += 1
...
>>> color_counts
{'brown': 3, 'green': 1, 'yellow': 2, 'red': 1, 'black': 1}
Python 2.4.6 (#2, Jan 24 2014, 23:57:03)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> colors = ["Brown", "Red", "Green", "White", "Yellow", "Yellow", "Brown", "Brown", "Black"]
>>> color_counts = dict((c, colors.count(c)) for c in set(colors))
>>> color_counts
{'Brown': 3, 'Yellow': 2, 'Green': 1, 'Black': 1, 'White': 1, 'Red': 1}
Python 2.5.6 (r256:88840, Oct 21 2014, 22:49:55)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import defaultdict
>>> colors = ["brown", "red", "green", "yellow", "yellow", "brown", "brown", "black"]
>>> color_counts = defaultdict(int)
>>> for c in colors:
... color_counts[c] += 1
...
>>> color_counts
defaultdict(<type 'int'>, {'brown': 3, 'green': 1, 'yellow': 2, 'red': 1, 'black': 1})
Python 2.7.10+ (default, Oct 10 2015, 09:11:24)
[GCC 5.2.1 20151003] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Counter
>>> colors = ["Brown", "Red", "Green", "White", "Yellow", "Yellow", "Brown", "Brown", "Black"]
>>> color_counts = Counter(colors)
>>> color_counts
Counter({'Brown': 3, 'Yellow': 2, 'Green': 1, 'Black': 1, 'White': 1, 'Red': 1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment