Python Random Tips #5

This is not a Python Starter Tutorial but advanced Python Tips in a Random Order.

What is Python Counter?

Python Counter is a container that keeps a count of the number of occurrences of any value in the container.

Python Counter helps to count the key-value pairs in an object.

With the inputs such as list, tuple, dictionary and string, the Counter gives us the count of every element.

#1 List Example:

We have the list with 4 elements such as a, b, c and d. When you use Counter on this list , it will count how many times a, b, c or d is present. As you can see we have got ‘a’ 3 times, ‘b’ 3 times, ‘c’ 2 times etc., in the result.

#2 Below is the example for String.

Please note that we have got 2 ‘blank’ spaces, 2 times the character ‘i’, 2 ‘n’ etc., in the given string ‘input_string’.

#3 Will this work for dictionary? Yes.

In the dictionary with Counter, the elements will be the keys and the count of elements will be the values. Please review the below example:

Please note that the display is sorted by the values (descending).

#4 Counter will work with arithmetic as well.

Lets use the below 3 inputs for our arithmetic calculations:

What about add? first + second.

If you notice five, four, one, two are available in the result as is but ‘three’ got the value 6 ( addition of 3 from first and another 3 from second).

Lets do ‘second + first’, will there be any difference in the output?

#5 Can we now try ‘subtraction’?

The common keys ‘four’ : 4 and ‘three : 3 got subtracted to 0 hence they are not listed in the output. Remaining ‘two’: 2 is the output for the operation first – third.

Please note that ‘five’:5 will not be part of the output for first – third operation.

If we do the third – first operation, we will get ‘five:5’ (from third) and not ‘two’: 2 (from the first). Did you get the logic?

More examples on ‘subtraction’:

‘four’ and ‘two’ are getting listed for the result (from first – second). ‘five’ and ‘one’ are getting listed for the result operation (second – first).

We have got ‘rand1’: 1 in the (first – second operation) from rand1: 5 minus rand1: 4.

#6 What about & (Intersection).

What will the result of first & second (intersection)?

Obviously ‘three’ is the common between ‘first’ and ‘second’ hence the result is ‘three’: 3. But what about if they have different values in ‘common’ key?

‘rand1’ is obviously the common element hence the result but why the ‘rand1’: 4 is getting displayed (and NOT ‘rand1’: 5?).

Simple. The intersection result is just a positive minimum value (not the maximum value hence ‘rand1’ : 4 is taken and ‘rand1’: 5 is NOT taken).

#7 Next operation is ‘union’ (|). Lets see the results as below:

As you can see the result is ‘union’ of all elements from both ‘first’ and ‘second’. If you notice ‘rand1’: 5 is getting into the result (and NOT ‘rand1: 4). That is the ‘maximum’ from the common key value is used in the | result).

That is, for Union (|), the result will be union of all elements and the positive maximum value from the common element.

That’s all for now. Let’s see you in the next post.

Leave a comment

Design a site like this with WordPress.com
Get started