map-filter-reduce


听说函数式编程很⑥,咱也不知道,咱也不晓得,还没实际用过。emmm。。。。,先mark下Python中和函数式编程有关的部分功能先,又开始水了,立个flag🚩:慢慢完善

map

先看下Python官方文档的说法

map(function, iterable, …),返回一个将 function 应用于 iterable 中每一项并输出其结果的迭代器。 如果传入了额外的 iterable 参数,function 必须接受相同个数的实参并被应用于从所有可迭代对象中并行获取的项。

见识一下

>>> def cook(something):
...     if something == "cow":
...         return "hamburger"
...     elif something == "tomato":
...         return "chips"
...     elif something == "chicken":
...         return "ddrumstick"
...     elif something == "corn":
...         return "popcorn"
...
>>> list(map(cook, ["cow", "tomato", "chicken", "corn"]))
['hamburger', 'chips', 'ddrumstick', 'popcorn']

filter

也看下官方文档的说法

filter(function, iterable),用 iterable 中函数 function 返回真的那些元素,构建一个新的迭代器。iterable 可以是一个序列,一个支持迭代的容器,或一个迭代器。如果 function 是 None ,则会假设它是一个身份函数,即 iterable 中所有返回假的元素会被移除。

也见识下

>>> def isVegetarian(food):
...     check = ['chips', 'popcorn']
...     if food in check:
...         return True
...     else:
...         return False
...
>>> list(filter(isVegetarian, ['hamburger', 'chips', 'ddrumstick', 'popcorn']))
['chips', 'popcorn']

reduce

再看下官方文档

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to >reduce the iterable to a single value.

见识下

>>> from functools import reduce
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15

一图胜千言

曾看到过一张把filter、map、reduce描述得很透彻得图,真滴六🐂

references


文章作者: ShanSan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ShanSan !
 上一篇
Serialization and Deserialization Serialization and Deserialization
序列化与反序列化 Serialization:Data Structure/Object –> Binary StringDeserialization:Binary String –> Data Structure/Objec
2019-11-25
下一篇 
git clone后如何checkout到remote branch git clone后如何checkout到remote branch
what/why通常情况使用git clone github_repository_address下载下来的仓库使用git branch查看当前所有分支时只能看到master分支,但是想要切换到其他分支进行工作怎么办❓ 其实使用git c
2019-10-27
  目录