-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctools-try.py
83 lines (67 loc) · 2.34 KB
/
functools-try.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
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
lambda operator or lambda function is used for creating small,
one-time and anonymous function objects in Python. lambda operator
can have any number of arguments, but it can have only one expression.
It cannot contain any statements and it returns a function object which
can be assigned to any variable.
'''
'''
The function taken by reduce takes two args and returns one value. reduce applies this function to the seq iteratively until
there is one value left to be returned.
'''
def test_reduce(seq):
from functools import reduce
print(reduce(lambda x, y: x+y, seq))
'''
reduce is the hardest to be substituted in reduce, filter and map, because you need to find another new method to substitute it.
Here sum() is a very handy substitute.
'''
'''
also equal to add(seq[0],add(seq[1],add(seq[2],add(seq[3], ... ))))
'''
def substitute_reduce(seq):
print(sum(seq))
'''
filter returns a filter object that is iterable and can be transformed into a list using list()
The function used by filter returns a boolean val, True or False.
Based on this function, filter builds a new iterable object that contains all the items in the seq that produces True
in the function.
'''
def test_filter(wordseq):
def short_word(word):
if len(word) < 5:
return True
print(list(filter(short_word, wordseq)))
'''
filter can be perfectly substituted by the list generator.
'''
def substitute_filter(wordseq):
def short_word(word):
if len(word) < 5:
return True
print([word for word in wordseq if short_word(word)])
'''
map returns a map object that is also iterable.
The function taken by map takes one arg and returns one value.
map function produces the result of each item in seq through the function, and builds a list of the results.
'''
def test_map(seq, wordseq):
def glue(x):
return str(x[0])+x[1]
print(list(map(glue, zip(seq, wordseq))))
'''
map can be perfectly substituted by list generator.
'''
def substitute_map(seq, wordseq):
def glue(x):
return str(x[0]) + x[1]
print([glue(pair) for pair in zip(seq, wordseq)])
if __name__ == '__main__':
nums = [1, 2, 3, 4]
words = ['apple', 'pear', 'banana', 'haw', 'fig']
test_reduce(nums)
substitute_reduce(nums)
test_filter(words)
substitute_filter(words)
test_map(nums, words)
substitute_map(nums, words)