- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmy_reduce.py
More file actions
Latest commit
21 lines (16 loc) · 478 Bytes
/
Copy pathmy_reduce.py
File metadata and controls
21 lines (16 loc) · 478 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
fromcollectionsimportIterable
defmy_reduce(func, seq, init=None):
```
参考官网例子('https://docs.python.org/3.5/library/functools.html?highlight=reduce#functools.reduce')
```
ifnotisinstance(seq, Iterable):
raise'The seq must be iterbale'
it=iter(seq)
ifinitisNone:
value=next(it)
else:
value=init
foriinit:
value=func(value, i)
returnvalue