Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Filter Function
Search
Eloy Zuniga Jr.
May 15, 2018
Programming
83
0
Share
The Filter Function
Basic understanding of the `filter` function.
Eloy Zuniga Jr.
May 15, 2018
More Decks by Eloy Zuniga Jr.
See All by Eloy Zuniga Jr.
The `max` function
eloy
0
66
The Command Line Interface
eloy
2
500
Python Decorators
eloy
8
640
Other Decks in Programming
See All in Programming
20260514_its_the_context_window_stupid.pdf
heita
0
1.1k
inferと仲良くなる10分間
ryokatsuse
1
250
OCRを使ってゲームのアイテムをデータ化する
kishikawakatsumi
0
110
Transactional Change Stream Processing With Debezium and Apache Flink
gunnarmorling
1
130
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
190
AI時代だからこそ「Bloc」を採用する価値があるのかもしれない
takuroabe
0
230
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
1
480
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
120
今さら聞けないCancellationToken
htkym
0
180
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
5
640
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
360
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
950
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
560
Are puppies a ranking factor?
jonoalderson
1
3.4k
Prompt Engineering for Job Search
mfonobong
0
320
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
750
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.5k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
250
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
140
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
230
New Earth Scene 8
popppiees
3
2.3k
Transcript
Handle Nonetype objects # Remove Nonetypes before using Max L
= [None, None, None] max([i for i in L if is not None], default=None) @eloy · python 3.6 · 2018 1/14
Using the filter function # Remove Nonetypes before using Max
max(filter(None, L), default=None) @eloy · python 3.6 · 2018 2/14
filter() Keep item if 'True'
Filter function # Takes a function and an iterable filter(<function>,
<iterable>) @eloy · python 3.6 · 2018 4/14
Let's try an example # Anything greater than 2 shall
pass def greater_than_2(item): """Assume item is integer.""" return item > 2 items = filter(greater_than_2, [1, 2, 3, 4]) @eloy · python 3.6 · 2018 5/14
Using the filter function # Remove Nonetypes before using Max
max(filter(None, L), default=None) @eloy · python 3.6 · 2018 6/14
@eloy · python 3.6 · 2018 7/14
Identity function # If NO function is passed, then we
use the "Identity function" filter(None, iterable) # Identity function = Is this Truthy or Falsey? truthy_items = [True, object, 'a', '0', 1, 2, -1] falsey_items = [False, None, '', "", 0] @eloy · python 3.6 · 2018 8/14
Testing identity function # Everything makes it through the filter
everything = filter(None, [True, object, 'a', 1]) # Nothing makes it through the filter nothing = filter(None, [False, None, '', "", 0]) @eloy · python 3.6 · 2018 9/14
Lets make a None function # Anything greater than 2
shall pass def fake_none(item): """Test truthiness.""" return bool(item) items = filter(fake_none, [1, 2, 3, 4]) @eloy · python 3.6 · 2018 10/14
Filter returns an iterable iterable = filter(None, truthy_items) @eloy ·
python 3.6 · 2018 11/14
Still an iterable iterable = list(filter(None, truthy_items)) @eloy · python
3.6 · 2018 12/14
Technically an iterator iterator = filter(None, truthy_items) @eloy · python
3.6 · 2018 13/14
Do you know the difference? 4 iterable 4 lazy iterable
4 iterator 4 range We'll talk about this next month :D @eloy · python 3.6 · 2018 14/14