forked from anumsh/Python-Programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
Latest commit
41 lines (31 loc) · 1.08 KB
/
Copy pathfilter.py
File metadata and controls
41 lines (31 loc) · 1.08 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# The built-in filter() function operates on any iterable type (list, tuple, string, etc).
# Python Filter with Number
numbers=[1, 6, 3, 8, 4, 9]
deflessThanFive(element):
returnelement<5
printfilter(lessThanFive, numbers) # [1, 3, 4]
a= [1,2,4,1,2,3]
s=set(a)
prints
defcheck(n):
ifnins:
returnTrue
else:
returnFalse
printfilter(check, a) #[1, 2, 4, 1, 2, 3]
"""
filter() takes two args: (fn, sequence), and returns a list.
The filter() will return all items from the list a which return True
when passed to the function check() which will check if the value
is in the set, s. Since all the numbers in the set come from the
values list, all of the original values in the list will return True.
"""
# Python Filter with String
names= ('angel', 'anushka', 'anum', '')
printfilter(None, names) # ('angel', 'anushka', 'anum')
# Python Filter with a Function
defstartsWithA(element):
iflen(element) >0:
returnelement[0] =='a'
returnFalse
printfilter(startsWithA, names) # ('angel', 'anushka', 'anum')