Share
Embed
Python's Counter collection
An introduction to Python's collections.Counter class. Sample code is available at https://gist.github.com/bradmontgomery/4717521.
Brad Montgomery
February 06, 2013
Transcript
40 slides
-
What is Counter? • It’s like a dictionary (keys &
values) • Keys = things you want to count • Values = the, uhm... count. Tuesday, February 5, 13 -
>>> from collections import Counter >>> c = Counter() #
Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13 -
>>> from collections import Counter >>> c = Counter() #
Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13 -
>>> from collections import Counter >>> c = Counter() #
Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13 -
# (most) regular dict methods are available >>> c.keys() ['widgets']
>>> c.values() [1] >>> 'widgets' in c True Tuesday, February 5, 13 -
# `update` will create new keys or # adjust the
counts for existing keys >>> c.update({'foo': 1}) >>> c Counter({'widgets': 1, 'foo': 1}) # calling `update` again will increment # the value of 'foo' >>> c.update({'foo': 1}) >>> c Counter({'widgets': 1, 'foo': 2}) Tuesday, February 5, 13 -
# `update` will create new keys or # adjust the
counts for existing keys >>> c.update({'foo': 1}) >>> c Counter({'widgets': 1, 'foo': 1}) # calling `update` again will increment # the value of 'foo' >>> c.update({'foo': 1}) >>> c Counter({'widgets': 1, 'foo': 2}) Tuesday, February 5, 13 -
# `update` will create new keys or # adjust the
counts for existing keys >>> c.update({'foo': 1}) >>> c Counter({'widgets': 1, 'foo': 1}) # calling `update` again will increment # the value of 'foo' >>> c.update({'foo': 1}) >>> c Counter({'widgets': 1, 'foo': 2}) Tuesday, February 5, 13 -
# You can create a Counter from an iterable >>>
c = Counter(['larry', 'moe', 'curly']) >>> c Counter({'larry': 1, 'curly': 1, 'moe': 1}) Tuesday, February 5, 13 -
# Or you can pass in keyword args >>> c
= Counter(ravens=34, niners=31) >>> c Counter({'ravens': 34, 'niners': 31}) Tuesday, February 5, 13 -
# The `elements` method gives you an iterator # that
yields a `key` for each`count` >>> colors = ['red', 'blue', 'yellow'] >>> c = Counter(colors) >>> c Counter({'blue': 1, 'yellow': 1, 'red': 1}) >>> c['red'] += 2 # Three 'red's >>> c['blue'] += 1 # Two 'blues's >>> c Counter({'red': 3, 'blue': 2, 'yellow': 1}) >>> list(c.elements()) ['blue', 'blue', 'yellow', 'red', 'red', 'red'] Tuesday, February 5, 13 -
# The `elements` method gives you an iterator # that
yields a `key` for each`count` >>> colors = ['red', 'blue', 'yellow'] >>> c = Counter(colors) >>> c Counter({'blue': 1, 'yellow': 1, 'red': 1}) >>> c['red'] += 2 # Three 'red's >>> c['blue'] += 1 # Two 'blues's >>> c Counter({'red': 3, 'blue': 2, 'yellow': 1}) >>> list(c.elements()) ['blue', 'blue', 'yellow', 'red', 'red', 'red'] { Tuesday, February 5, 13 -
# The `elements` method gives you an iterator # that
yields a `key` for each`count` >>> colors = ['red', 'blue', 'yellow'] >>> c = Counter(colors) >>> c Counter({'blue': 1, 'yellow': 1, 'red': 1}) >>> c['red'] += 2 # Three 'red's >>> c['blue'] += 1 # Two 'blues's >>> c Counter({'red': 3, 'blue': 2, 'yellow': 1}) >>> list(c.elements()) ['blue', 'blue', 'yellow', 'red', 'red', 'red'] { Tuesday, February 5, 13 -
# The `elements` method gives you an iterator # that
yields a `key` for each`count` >>> colors = ['red', 'blue', 'yellow'] >>> c = Counter(colors) >>> c Counter({'blue': 1, 'yellow': 1, 'red': 1}) >>> c['red'] += 2 # Three 'red's >>> c['blue'] += 1 # Two 'blues's >>> c Counter({'red': 3, 'blue': 2, 'yellow': 1}) >>> list(c.elements()) ['blue', 'blue', 'yellow', 'red', 'red', 'red'] { Tuesday, February 5, 13 -
# Finding the N "most common" elements >>> c Counter({'red':
3, 'blue': 2, 'yellow': 1}) >>> c.most_common(2) [('red', 3), ('blue', 2)] Tuesday, February 5, 13 -
# Finding the N "most common" elements >>> c Counter({'red':
3, 'blue': 2, 'yellow': 1}) >>> c.most_common(2) [('red', 3), ('blue', 2)] Tuesday, February 5, 13 -
# Trick: Find the most common letters in a string:
>>> word = 'supercalifragilisticexpialidocious' >>> Counter(word).most_common(3) [('i', 7), ('a', 3), ('c', 3)] Tuesday, February 5, 13 -
What are the 10 most common words in “The Wonderful
Wizard of Oz”? Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
from collections import Counter import re import urllib # use
requests! # Download the content url = 'http://bit.ly/thewonderfulwizard' content = urllib.urlopen(url).read() # Clean the content a little content = re.sub('\s+', ' ', content) content = re.sub('[^A-Za-z ]+', '', content) words = content.split() # Start counting word_count = Counter(words) word_count.most_common(10) Tuesday, February 5, 13 -
How can this help me build that RPG/MMOG I’ve always
wanted to create? Tuesday, February 5, 13 -
# Set up your inventory: >>> money = {'gold': 1000,
'silver': 500, 'copper': 100} >>> purse = Counter(money) >>> purse Counter({'gold': 1000, 'silver': 500, 'copper': 100}) Tuesday, February 5, 13 -
# Set up your inventory: >>> money = {'gold': 1000,
'silver': 500, 'copper': 100} >>> purse = Counter(money) >>> purse Counter({'gold': 1000, 'silver': 500, 'copper': 100}) Tuesday, February 5, 13 -
# Set up your inventory: >>> money = {'gold': 1000,
'silver': 500, 'copper': 100} >>> purse = Counter(money) >>> purse Counter({'gold': 1000, 'silver': 500, 'copper': 100}) Tuesday, February 5, 13 -
# Items in the shop: >>> shield = {'gold': 25}
>>> sword = {'gold': 100, 'silver':50} Tuesday, February 5, 13 -
# Items in the shop: >>> shield = {'gold': 25}
>>> sword = {'gold': 100, 'silver':50} # Buy a shield >>> purse.subtract(shield) >>> purse Counter({'gold': 975, 'silver': 500, 'copper': 100}) Tuesday, February 5, 13 -
# Items in the shop: >>> shield = {'gold': 25}
>>> sword = {'gold': 100, 'silver':50} # Buy a shield >>> purse.subtract(shield) >>> purse Counter({'gold': 975, 'silver': 500, 'copper': 100}) Tuesday, February 5, 13 -
# Items in the shop: >>> shield = {'gold': 25}
>>> sword = {'gold': 100, 'silver':50} # Buy a shield >>> purse.subtract(shield) >>> purse Counter({'gold': 975, 'silver': 500, 'copper': 100}) # Buy a sword >>> purse.subtract(sword) Counter({'gold': 875, 'silver': 450, 'copper': 100}) Tuesday, February 5, 13 -
# Items in the shop: >>> shield = {'gold': 25}
>>> sword = {'gold': 100, 'silver':50} # Buy a shield >>> purse.subtract(shield) >>> purse Counter({'gold': 975, 'silver': 500, 'copper': 100}) # Buy a sword >>> purse.subtract(sword) Counter({'gold': 875, 'silver': 450, 'copper': 100}) # Buy a Castle! >>> castle = {'gold': 50000, 'silver': 9999, 'copper': 350} >>> purse.subtract(castle) >>> purse Counter({'copper': -250, 'silver': -9549, 'gold': -49125}) Tuesday, February 5, 13 -
There’s more great stuff in collections! http://docs.python.org/2/library/collections.html http://docs.python.org/3.3/library/collections.html Sample code:
https://gist.github.com/bradmontgomery/4717521 Tuesday, February 5, 13