Usage of counters

So far our examples were demonstrated using Python IDLE GUI. Now we will see the usage of counter by creating a Python file. For those who did not understand what is a Python file, read Chapter 1, Getting Started with Python, and then you can come back and continue:

import collections 

c = collections.Counter('King TutanKhamen was the youngest Pharoah')
print c
for letter in 'What a king?':
print '%s : %d' % (letter, c[letter])

In the given example, we try to compare the stream of strings provided as input to the Counter with another stream of strings. In this case, Counter prints out only the frequency of the sequence of strings we want to compare and not the input string to the Counter:

Counter does not raise KeyError for unknown items. If a value is not found in the input string (as with W, k, and ? in this example), then its count is 0.

King Tutankhamen (or Tutankhamun) ruled Egypt as pharaoh for 10 years until his death at the age of 19, around 1324 B.C. He was barely known to the modern world until 1922, when British archaeologist, Howard Carter, chiseled through a doorway and entered the boy pharaoh’s tomb, which had remained sealed for more than 3,200 years. This information has been taken from http://www.history.com/topics/ancient-history/tutankhamen.

We will look at another example where we will deal with the basic file operations. Here, we will provide a text file, which has sentences and we will provide this file to counter as input. The following screenshot shows the text file with some texts that will serve as input to the counter:

Now, let's look at the code for passing the text file as the input to the Counter:

import collections

co = collections.Counter()
file_txt = open("Counter_input.txt","r")
for line in file_txt:
co.update(line.lower())
print co

The preceding program gives the frequency of all the characters present in the text file. We will use the open() function to open the file in read mode and file_txt variable serves as handle to this file handling operation. We will use the for loop and update our counter with inputs from the file one by one. The outcome is shown in the following screenshot:

There could be a scenario where you might require to choose the first five letters of higher frequency. Here you can achieve the desired outcome using the most_common() method.

Syntax

most_common(number) 

Let's take a look at the example for the preceding scenario:

import collections

co = collections.Counter()
file_txt = open("Counter_input.txt","r")
for line in file_txt:
co.update(line.lower())

print "Most common:n"
for letter, count in co.most_common(5):
print '%s: %7d' % (letter, count)

We have modified our example here and now we are trying to retrieve the first 5 letters of higher frequency. Here we have simply made use of the most_common() method of Counter. The outcome is shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.114.28