Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

How to Use the filter() Function

In this lesson, you’ll see how to usefilter(). The built-in functionfilter() is another classic functional construct and can be converted into alist comprehension.

It takes apredicate as a first argument and an iterable as a second argument. It builds an iterator containing all the elements of the initial collection that satisfies the predicate function.

00:02Next isfilter().filter() takes two arguments,afunction and aniterable.It constructs a new iterator from those elements of theiterablefor which the function returnsTrue.

00:17This allows quick creation of iterables of items in another iterable that havepassed a test.Here’s an example where we’re passing it the list of numbers[1, 2, 3, 4],and the lambda expression is onlyTrue when division by2 has no remainder—in other words, when the numbers are even. As a result,we create the new listeven, which only has the numbers2 and4 in it.

00:44Next you’ll seefilter() in action.So here, you will see a first example of usingfilter().We’re going to start out with a list,usingrange() to fill that, so we’ll have the numbers1 to20,and we’ll print that out so you’ll see it. And now comes the filtering.

01:10So.We’re going to create a new list calledevensand usefilter() with a lambda expression.lambda x isTrue whenx % 2 == 0.

01:30That detects whether it’s even or not.And we also pass in the listnums.Now we’re going to printevens out, and then running that,you’ll see how that works. So thereyou can see the two lists. The first one is all the numbers from1 to20,and the second new list is one created byfilter() and only contains the numberswhere the lambda expressionx % 2 == 0 isTrue.

02:02In this case, that’s the even numbers.We can extend that idea a little.Make a new list as well,wherex % 2 != 0.

02:26And let’s see that running.And there you can see the third list contains only the odd numbers.The expression wasTrue, but this time not for equality with0,but inequality with0.

02:44Here’s a second example of usingfilter(), where you’re going to see checking for thevalue being above the mean of a list of values.We’ll start out by importing themean() function from thestatistics module,and now create ourdata.

03:05We’ll calculate the average usingmean(),

03:13and then create our above average list usingfilter() and a lambda expression.

03:26And finally, we’ll print out our original list,the average value,and our new list.Let’s see that program in action.And there you see the list1 to20,the average value of10.5,and then our new list of the values that are above the average.

03:56Again,that could be extended to give us a below average list using a new lambdaexpression.

04:14And running it produces what we’d expect, with two new lists—onewith the above average valuesand one with the below average values.The previousfilter() examples you’ve seen have been working on numbers. However,the lambda expression can work on any kind of input, so hereyou’re going to see an example with it working on text. Firstly,we have our list of input data—

04:57in this case, baseball team nicknames.We’ll print that original list out,and now create a new list usingfilter().

05:19Here, the lambda expressionis going to beTrue when the length ofx is less than6.

05:37Finally, we’ll pass in thenicknames list,and our expression is complete. Now we can print out the new list of names,and let’s see that code running.

05:55Here’s the original list, and you can see that the only elements of thatwhere the lambda expression wasTrue and the length was less than6 was'Cubs'and'Reds', and those make up the new list.

Avatar image for John Laudun

John Laudun onSept. 3, 2019

This has been really useful for someone like me who is slowly making his way from novice to something like apprentice with aspirations for craftsman. I am curious about one very small bit: if the goal of lambda functions is to have things in one line, why notabove_avg = list(filter(lambda x : x > mean(nums), nums))?

i suspect because mean(nums) would be evaluated len(nums) times

Become a Member to join the conversation.

Course Contents

Overview
54%

[8]ページ先頭

©2009-2026 Movatter.jp