Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python's Counter collection

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
Tweet

More Decks by Brad Montgomery

Other Decks in Programming

Transcript

  1. What is Counter? • It’s like a dictionary (keys &

    values) • Keys = things you want to count • Values = the, uhm... count. Tuesday, February 5, 13
  2. >>> from collections import Counter >>> c = Counter() #

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

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

    Create a Counter >>> c['widgets'] += 1 # start counting 'widgets' >>> c Counter({'widgets': 1}) Tuesday, February 5, 13
  5. # (most) regular dict methods are available >>> c.keys() ['widgets']

    >>> c.values() [1] >>> 'widgets' in c True Tuesday, February 5, 13
  6. # `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
  7. # `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
  8. # `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
  9. # 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
  10. # Or you can pass in keyword args >>> c

    = Counter(ravens=34, niners=31) >>> c Counter({'ravens': 34, 'niners': 31}) Tuesday, February 5, 13
  11. # 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
  12. # 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
  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
  14. # 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
  15. # 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
  16. # 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
  17. # 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
  18. What are the 10 most common words in “The Wonderful

    Wizard of Oz”? Tuesday, February 5, 13
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. How can this help me build that RPG/MMOG I’ve always

    wanted to create? Tuesday, February 5, 13
  27. # 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
  28. # 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
  29. # 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
  30. # Items in the shop: >>> shield = {'gold': 25}

    >>> sword = {'gold': 100, 'silver':50} Tuesday, February 5, 13
  31. # 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
  32. # 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
  33. # 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
  34. # 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