Slide 1

Slide 1 text

collections.Counter Python’s Tuesday, February 5, 13

Slide 2

Slide 2 text

What is Counter? • It’s like a dictionary (keys & values) • Keys = things you want to count • Values = the, uhm... count. Tuesday, February 5, 13

Slide 3

Slide 3 text

>>> from collections import Counter >>> c = Counter() # Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13

Slide 4

Slide 4 text

>>> from collections import Counter >>> c = Counter() # Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13

Slide 5

Slide 5 text

>>> from collections import Counter >>> c = Counter() # Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13

Slide 6

Slide 6 text

# (most) regular dict methods are available >>> c.keys() ['widgets'] >>> c.values() [1] >>> 'widgets' in c True Tuesday, February 5, 13

Slide 7

Slide 7 text

# `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

Slide 8

Slide 8 text

# `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

Slide 9

Slide 9 text

# `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

Slide 10

Slide 10 text

# 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

Slide 11

Slide 11 text

# Or you can pass in keyword args >>> c = Counter(ravens=34, niners=31) >>> c Counter({'ravens': 34, 'niners': 31}) Tuesday, February 5, 13

Slide 12

Slide 12 text

# 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

Slide 13

Slide 13 text

# 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

Slide 14

Slide 14 text

# 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

Slide 15

Slide 15 text

# 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

Slide 16

Slide 16 text

# 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

Slide 17

Slide 17 text

# 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

Slide 18

Slide 18 text

# 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

Slide 19

Slide 19 text

What are the 10 most common words in “The Wonderful Wizard of Oz”? Tuesday, February 5, 13

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

How can this help me build that RPG/MMOG I’ve always wanted to create? Tuesday, February 5, 13

Slide 28

Slide 28 text

Answer: Let’s go shopping! Tuesday, February 5, 13

Slide 29

Slide 29 text

# 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

Slide 30

Slide 30 text

# 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

Slide 31

Slide 31 text

# 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

Slide 32

Slide 32 text

# Items in the shop: >>> shield = {'gold': 25} >>> sword = {'gold': 100, 'silver':50} Tuesday, February 5, 13

Slide 33

Slide 33 text

# 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

Slide 34

Slide 34 text

# 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

Slide 35

Slide 35 text

# 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

Slide 36

Slide 36 text

# 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

Slide 37

Slide 37 text

oops! you’ll have to handle over-spending yourself! Tuesday, February 5, 13

Slide 38

Slide 38 text

>>> purse.clear() Counter() Tuesday, February 5, 13

Slide 39

Slide 39 text

Thanks! >>> purse.clear() Counter() Tuesday, February 5, 13

Slide 40

Slide 40 text

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