Python - Week 8 - Dictionaries, Tuples, Sorting and Filtering

I spent last week understanding Python's sorting with Tuples and Dictionaries.

Here is the first snippet that demonstrates how tuples are sorted.

You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and score are numbers.
The tuples are input by console. The sort criteria is:
1: Sort based on name;
2: Then sort based on age;
3: Then sort by score.
The priority is that name > age > score.
If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]

Here is the code:

The code looks pretty concise, as if not much has been accomplished. But there are few points worth mentioning:

  1. I am passing a list of tuples, each tuple representing name, age, height triplet. This is also the expected output format.
  2. We want to sort the input list, and that is what we see in Line 9.
  3. The crux of program is itemgetter call which constructs a callable that assumes an iterable object - a list of tuples in our case - as input. We are passing three parameters to this, which are indexes of tuple on which we want to sort - 0,1,2 as the position of sort parameters in tuple. Had we wanted to sort on name > score > age, we would have passed 0,2,1. 

Here is another program that does filtering of string and then uses dictionary.


Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.
Suppose the following input is supplied to the program:
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1

Below is the program snippet, but I have digressed a bit by accepting lines from file. This helps me exercise file handling basics for the first time.


Key program highlights:

  1. We used lists in the past. Lists provide ordered collections of other objects, while dictionaries store objects by key; both lists and dictionaries may be nested, can grow and shrink on demand, and may contain objects of any type.
  2. An expression wrapped in square brackets makes a list, one in curly braces makes a dictionary (Line 3), one in single brackets makes a strings.
  3. {} is a dictionary in Pythons. Empty sets must be created with the set built-in, and print the same way.
  4. Text files represent content as normal strings and perform Unicode encoding and decoding automatically when writing and reading data.
  5. Line 7 - By default the file opens in read mode. Also, it reads a line at a time.
  6. Line 9 - filter selects items for which a function is true. In our case the function is None, which in this context basically removes empty lines.
  7. Line 11 - As we have seen in earlier posts, this for loop picks a character at a time.
  8. This is followed by a standard if clause which either increments the count if it finds that the word exists in dictionary or it initializes the word count to 1 when found for the first time.
  9. The last highlight is how to use try/except clause which here checks for the valid filename. I am sure there are more robust use cases for try/except which I hope to encounter in future.

Comments

Popular posts from this blog

Python - Week 3 - Lists, Tuples, Slicing, Math Functions

Python - Week 5 - Comprehensions