-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.py
More file actions
27 lines (19 loc) · 1.44 KB
/
filter.py
File metadata and controls
27 lines (19 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Filters out items based on a condition. You will be handing a lambda function to the filter function.
# The following takes a list and processes the list items through the lambda function, returning just the even numbers.
l = [1, 2, 3, 4]
evens = list(filter(lambda x: x % 2 == 0, l))
print(evens)
# The following checks the first letter in the names from the list, and returns names beginning with the letter f
# The premise of these is that it checks if the condition is true or false. Those conditions that are true are added to the a_names list.
names = ["Frankfurt", "Fiona", "Flavor Flav", "Ralph"]
a_names = list(filter(lambda x: x[0]=="F", names))
print(a_names)
# Combine map and filter. In this scenario, we are going to map through items in the list, and then after that filter those down by the condition.
# I created an exercise in which we will have a name and a grade. Those with at least 70 points will pass and gain a congratulations print-out.
grades = [{"name": "Falon", "grade": 70}, {"name": "Marie", "grade": 45}, {"name": "JJ", "grade": 90}]
passing_grades = list(map(lambda x: x["name"],filter(lambda grade: grade["grade"] >= 70, grades)))
# the previous can be written as a list comprehension, which is preferred in Python
comp_passing_grades = [name["name"] for name in grades if name["grade"] >= 70]
print(passing_grades)
print(comp_passing_grades)
# If you can use list comprehension, use it. Map and filter may not be used as much.