Programmer's Python Data - Named Tuples and Counters
Written by Mike James   
Tuesday, 03 September 2024
Article Index
Programmer's Python Data - Named Tuples and Counters
Counter

Counter

The Counter class implements what is generally called in math a multiset, a modification of the concept of a set that, unlike a set, allows for multiple instances for each of its elements. In other languages this is called a bag or multiset. The idea is simply that a Counter object is a dictionary where the values are counts of how many times the key has occurred. A set can only contain an element once but for a Counter the element can occur multiple times indicated by its count.

You can create a Counter object by using its constructor either with an iterable or a dictionary in which case the key values are used as key counts. For example:

count = Counter({"mike":3,"harry":2,"lucy":1})
print(count)

displays:

Counter({'mike': 3, 'harry': 2, 'lucy': 1})

which indicates that mike occurs three times, harry twice and lucy once.

If you supply an iterable then it is used to construct a frequency count of the elements. For example:

myList = ["harry","mike","lucy","mike","harry","mike"]
count = Counter(myList)
print(count)

displays:

Counter({'mike': 3, 'harry': 2, 'lucy': 1})

as before.

The elements in a Counter have to be hashable and the counts can be negative or zero.

A Counter instance acts like a dictionary, but it returns a count of zero rather than an exception if the key isn’t present.

The idea that a Counter is a bag, i.e. a set with possibly repeated elements, is supported by the elements() method which returns an iterator which runs over the elements including the repeats implied by the count values. For example:

print(list(count.elements()))

where count is as in the previous example, displays:

['harry', 'harry', 'mike', 'mike', 'mike', 'lucy']

Notice that the order of the elements is the same as they were encountered in the iterable. Using the iterator in a for loop means that the loop repeats for as many times as each element is in the bag.

There are some useful methods for working with Counter instances as frequencies:

  • most_common(n)
    returns the n most common elements as a list of tuples in the form (key, count)

  • total()
    returns a total of the counts

  • subtract(iterable)
    subtracts the counts from the existing Counter.

There are also operators that combine Counter instances in useful ways:

  • a+b adds a and b, i.e. a[key]+b[key]

  • +b is the same as +b[key]

  • a-b subtracts b from a, i.e. a[key]-b[key]

  • -b is the same as -b[key]

  • a & b intersection of a and b, i.e. min(a[key],b[key])

  • a | b union of a and b i.e. max(a[key],b[key])

All of these operations remove elements with counts of zero or less.

In chapter but not in this extract

  • Binary Tree
  • Complete Listing

Summary

  • The LIFO stack is a fundamental data structure and it can be implemented using a Python list.

  • The key feature of a LIFO stack is that it reverses the order of items added to it.

  • The queue, or FIFO stack, is also fundamental and it too can be implemented using a Python list, or more efficiently using a Python deque.

  • A queue does not change the order of items added to it.

  • A double-ended queue or deque is implemented using the Python deque.

  • A deque has characteristics of a LIFO and a FIFO stack.

  • Named tuples are similar to structs or records in other languages.

  • Named tuples are immutable.

  • The Counter is similar to what other languages call a bag or a multiset. It works like a dictionary or set, but stores the count of the number of times each element has been added.

  • Python does not have a standard binary tree data structure. However, adding one is easy and an excellent example of using stacks and queues.

Programmer's Python
Everything is Data

Is now available as a print book: Amazon

pythondata360Contents

  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
  5. Sequences, Lists & Tuples
       Extract Sequences 
  6. Strings
       Extract Unicode Strings
  7. Regular Expressions
  8. The Dictionary
       Extract The Dictionary 
  9. Iterables, Sets & Generators
       Extract  Iterables 
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
       Extract Stacks, Queues and Deques
      
    Extract Named Tuples and Counters***NEW!!!
  12. Bits & Bit Manipulation
       Extract Bits and BigNum 
  13. Bytes
       Extract Bytes And Strings
       Extract Byte Manipulation 
  14. Binary Files
  15. Text Files
  16. Creating Custom Data Classes
        Extract A Custom Data Class 
  17. Python and Native Code
        Extract   Native Code
    Appendix I Python in Visual Studio Code
    Appendix II C Programming Using Visual Studio Code

<ASIN:1871962765>

<ASIN:1871962749>

<ASIN:1871962595>

<ASIN:B0CK71TQ17>

<ASIN:187196265X>

Related Articles

Creating The Python UI With Tkinter

Creating The Python UI With Tkinter - The Canvas Widget

The Python Dictionary

Arrays in Python

Advanced Python Arrays - Introducing NumPy

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner



Last Updated ( Tuesday, 03 September 2024 )