-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_example.py
68 lines (44 loc) · 1.03 KB
/
filter_example.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Filter Function
# filter(fun, iterables)
# returns true of it matches the filter
# Sub range
import random
v = []
for x in range(10):
v.append(random.randrange(100))
print(v)
def lower(value):
if value < 50:
return True
else:
return False
f = filter(lower, v)
print(f'Less Than 50: {list(f)}')
class Animal:
name = ''
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init__(self, name):
super().__init__(name)
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
animals = []
for x in range(10):
name = 'Animal' + str(x)
if (x % 2) == 0:
animals.append(Cat(name))
else:
animals.append(Dog(name))
print(animals)
for a in animals:
print(f'Animal:{a.name}')
def cats(value):
return isinstance(value, Cat)
def dogs(value):
return isinstance(value, Dog)
for c in list(filter(cats, animals)):
print(f'Cat:{c.name}')
for d in list(filter(dogs, animals)):
print(f'Dog:{d.name}')