Lesson ยท 16 min
Filter and pick
A module of your own
From this lesson on, main.py does not read the file itself. The lesson has a file named bikedata.py. It holds the function load_trips that you wrote in the last lesson. A Python file that other code imports is a module, the same as csv.
from bikedata import load_trips
trips = load_trips("trips.csv")
from bikedata import load_trips loads one name from the module. You then call load_trips without the name of the module in front of it. Select the tab bikedata.py to read the code.
Filter: keep some rows
Most questions about data start with the words "which rows". You know the loop that answers them.
long_trips = []
for trip in trips:
if trip["minutes"] > 30:
long_trips.append(trip)
Python has a short form for this loop. Its name is list comprehension.
long_trips = [trip for trip in trips if trip["minutes"] > 30]
Read it in three parts, and start in the middle:
for trip in tripstakes each trip.if trip["minutes"] > 30keeps a trip only when the condition is true.- The first
tripis the value that goes into the new list.
The list trips does not change. You get a new list.
Pick: keep one value of each row
The first part can be any value. Here it is one column.
starts = [trip["start"] for trip in trips]
You can filter and pick in one step.
member_minutes = [trip["minutes"] for trip in trips if trip["rider"] == "member"]
Join two conditions with and or or, as you do in an if statement.
Numbers from a list
| Code | Result |
|---|---|
len(values) |
the number of items |
sum(values) |
the total of the numbers |
min(values) |
the smallest value |
max(values) |
the largest value |
round(7.4567, 1) |
7.5, the number with 1 digit after the decimal point |
print(sum(member_minutes) / len(member_minutes)) # the average
The length of an empty list is 0, and Python cannot divide by 0. When a list can be empty, check that first.
Each value one time
A set keeps each value one time. set(starts) drops the names that repeat. A set has no order. sorted takes the values and gives a new list in order.
stations = sorted(set(starts))
Your turn
main.py already loads the 16 trips of trips.csv. Do the tasks in order. Two tasks ask for a function. The checker calls each function with other trips, so use the parameters of the function, and not the 16 trips of the file.
Tasks
5 to do
Task 1, not checked yet. Make a list
long_tripswith the trips that took more than 30 minutes. Use a list comprehension.Task 2, not checked yet. Make a variable
electric_minuteswith the total minutes of the trips on an electric bike.Task 3, not checked yet. Make a list
stationswith the name of each start station one time, in alphabetical order.Task 4, not checked yet. Write a function
trips_from(trips, station). It returns the list of the trips that start atstation.Task 5, not checked yet. Write a function
share(trips, rider). It returns the percent of the trips that have this rider, with 1 digit after the decimal point. For an empty list, it returns0.
Task 1 of 5
Make a list long_trips with the trips that took more than 30 minutes. Use a list comprehension.