Skip to Content
Secret Recipes of the Python Ninja
book

Secret Recipes of the Python Ninja

by Cody Jackson
May 2018
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 37m
English
Packt Publishing
Content preview from Secret Recipes of the Python Ninja

How to do it...

  1. Here is how to create a new Counter object, as demonstrated from https://docs.python.org/3/library/collections.html#collections.Counter:
      >>> from collections import Counter      >>> c = Counter() # a new, empty counter      >>> c = Counter('gallahad') # a new counter from an iterable      >>> c = Counter({'red': 4, 'blue': 2}) # a new counter from a mapping      >>> c = Counter(cats=4, dogs=8) # a new counter from keyword args
    • The first object is simply an empty counter, much like creating an empty dictionary. The second Counter creates a mapping of a text string, summing the count of each unique letter, is as follows:
                   >>> c                   Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1})
    • The third Counter object is a direct creation from a dictionary, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Expanding World of Python

The Expanding World of Python

Dane Hillard

Publisher Resources

ISBN: 9781788294874Supplemental Content