Here are resources and additional documentation about tuples, packing, and unpacking:
Combining map() With filter()
Let’s start off with
filter() function takes two positional arguments. The first one is a function, and the function is going to return either
False or some object that Python can convert to a Boolean.
00:40 Let’s see a quick example of this. All right, so in this example, you’ve got a list of strings and you want to convert the strings in this list that are considered integers because you then want to go ahead and do some sort of computations with these values.
And so what you want to do is you want to filter out all of these types of strings. And so with the
filter() function what we’ll do is we’re going to pass in
data as the iterable and then use the
.isnumeric() function from the
str class to only keep those strings that are considered numeric.
The iterator returned by filter, we’re going to save it in the
numeric_data variable, and then we’re going to take that
numeric_data iterator and use it as our iterator into a
map() call where the function is going to be the built-in
int() function in Python.
This will take the
numeric_data iterator and convert all of those strings into integers. And then if we call the
list() function just to see what the final list is, we get the list containing the integers
Let’s take a look at an example that combines the
filter() and the
map() function. In the example, we’re going to be working with colors, and in particular we’re going to be using the RGB color model. In the RGB color model, you mix a little bit of red, a little bit of green, and a little bit of blue to specify the color that you’re after. These are called the RGB channels, and a common way to specify these values is to give an integer between
255. So for example, the tuple
(255, 0, 0) specifies the color red.
Whereas, for example, the tuple
(0, 0, 255) specifies the color blue. Now, to get orange, for example, you have to mix all the red and about halfway of the green. Now, as a side note, because each of the channels can take on a value between
255, you can produce with the RGB model, theoretically, 256 to the power of 3 different colors, which is around 16.8 million different colors.
So, for example, for the first color of
(200, 120, 40), your application accepts only percentages for the channels, and so what you need to do is convert each of these channels to a float between
1.0, and so the color would be specified with this tuple of floats.
04:37 Now, I’m not an expert in color theory, but if I was asked to just sort of give an elementary way to determine when a color is considered greenish, what I’d probably do is say, “Well, if the green channel is higher than the blue and the red channel, then I’m going to consider that RGB color greenish.” So, that’s what we want to do.
All right, let’s write out our filter function. So, the filter function—let’s call it, say,
isgreenish(). Remember that the function that you’re going to pass into the filter function should return a Boolean.
05:31 If you’re unfamiliar with how Python unpacking works, I’ll leave a link so you can look it up. But basically what we’re doing is this tuple that’s going to be the input will be a tuple containing three elements.
And then what we simply want to do is return whether
g is greater than
g is greater than
b. This is going to be our way to decide whether an RGB color is greenish. All right, so that’s our filter function.
So, why not use the
map() function to do that? We’ll use a
lambda function. It’s going to take one input, it’s going to represent the channel, and we just simply want to return the channel value divided by
255, and we want to do this for each of the elements in the
06:48 And so that we don’t get a lot of decimal places, why don’t we round this float so that only two digits after the decimal are kept? Now, as you know, this is going to return an iterator, and so what we’ll do at the very end is create a tuple out of it so that we get a tuple of RGB channels that are in percentage, and that’s going to be our final return value.
So, this is our
normalize() function. All right, so let’s clear that up. Let’s go back here. We’ve got our
colors. What we wanted to do is we wanted to map our normalization function to the colors that are greenish, and so let’s filter out and keep only the colors that are greenish on the list of our colors.
All right. I hope you enjoyed the lesson on how to combine the
map() function with the
filter() function. In the next lesson, we’ll take a look at how we can do something similar by combining the
map() function with the
Become a Member to join the conversation.