Very often we need to deal with sorting data in our Python projects. Python provides the sorted() function that can sort any sequence types that support pair-wise comparison. Most of the time, we can use this sorted() function directly when we have unified and simple data types like numbers or strings. However, in some cases we have a more complex data structure and this sorted() won't work as we expected.
Today, we will review some basic about sorting and explore some typical sorting problems that we regularly face in our real world data science project.
But first of all, let's create some simple collections and understand how the basic sorted() function works.
Here is the built-in help content of the sorted() function.
Below is a simple list of numbers.
We will sort it.
We can also sort string.
Those two are the most basic and common of sort. However, in reality, we usually need to sort a more complex data structure than just a simple list of numbers or string.
For example, we have below a list of tuples of movies and their rating. Sometimes we need to sort by movies titles, while sometimes we want to sort by rating.
We want to sort this list by movie titles. To do this we use the `key` param of the sorted() function to provide the sort key by providing a lambda function.
Now we want to sort the list by rating with the highest rated movies at top.
Let's solve another common sorting problem. We have a list of dictionaries. Each dictionary contains data about a movie like: title, revenue, cost.
Now, we want get a list of movie titles that has revenues from highest to lowest.
We now want the movie title and its associated cost of the movie that has the lowest cost.
Hope you finds this series helpful.
Happy sorting!
Comments