1 介绍
https://docs.python.org/zh-cn/3/howto/functional.html
函数式 编程将一个问题分解成一系列函数。
理想情况下,函数只接受输入并输出结果,对一个给定的输入也不会有英系那个输出内部状态。著名的函数式语言有ML家族和Haskell。
2 高阶函数
2.1 map/reduce
Python内建了map()
和reduce
函数。
该函数的思想同Google发布的“MapReduce: Simplified Data Processing on Large Clusters”相同。
2.1.1 map方法
eg. f(x)=x^2
(1)用map方式实现
def f(x):
return x * x
r = map(f, [1, 2, 3])
print(list(r))
(2)传统方式
这种方法将 f(x)作用到了list的每一个元素,结果在新的list
def f(x):
return x * x
L = []
for n in [1, 2, 3]:
L.append(f(n))
print(L)
eg. 将list所有数字转为字符串
print(list(map(str, [1, 2, 3])))
2.1.1 reduce方法
reduce
把一个函数作用在以下序列 [x1, x2, x3,…] 上,这个函数必须介绍2个参数, reduce
把结果继续和序列的下一个元素做积累计算,其效果为
reduce(f,[x1, x2, x3]) = f(f(f(f(x1, x2),x3),x4)
序列求和
from functools import reduce
def add(x, y):
print(x, y)
return x + y
print(reduce(add, [1, 2, 3]))
print(sum([1, 2, 3]))
把序列[1, 3, 5, 7, 9]变换成整数13579
from functools import reduce
def fn(x, y):
return x * 10 + y
print(reduce(fn, [1, 3, 5, 7, 9]))
2.2 filter(筛选)
Python内包含filter()
函数用于过滤序列。
与map()
不同的是filter()
把传入的函数依次作用于每个元素,然后根据返回值值True
或False
保留或删除元素。
filter返回的是一个Iterator, 是个惰性序列,所以要转为list
eg. 删除list中偶数,保留奇数
def is_odd(n):
return n % 2 == 1
print(list(filter(is_odd, [1, 2, 3, 4, 5, 6])))
eg. 删除序列中的空字符串
def not_empty(s):
return s and s.strip()
print(list(filter(not_empty, ['A', '', 'C', None, ' '])))
3 匿名函数 lambda
lambda
:表示匿名函数,冒号前表示参数,冒号后为表达式(只能有一个)
eg. 求解f(x)=x^2
(1) map()
函数
def f(x):
return x * x
r = map(f, [1, 2, 3])
print(list(r))
(2)lambda
方法
print(list(map(lambda x: x * x, [1, 2, 3])))
4 装饰器
def use_logging(func):
def wrapper(*args, **kwargs):
print(">>>>>>>>>>>>>>>")
print("%s 正在运行" % func.__name__)
print(*args)
print(args[0])
print(args[1])
print(">>>>>>>>>>>>>>>")
return func(*args)
return wrapper
@use_logging
def getInfo(name, age):
print("-------")
print(name, age)
getInfo("有勇气的牛排", "20")
<h2><a id="1__0"></a>1 介绍</h2>
<p>https://docs.python.org/zh-cn/3/howto/functional.html</p>
<p><strong>函数式</strong> 编程将一个问题分解成一系列函数。</p>
<p>理想情况下,函数只接受输入并输出结果,对一个给定的输入也不会有英系那个输出内部状态。著名的函数式语言有ML家族和Haskell。</p>
<h2><a id="2__7"></a>2 高阶函数</h2>
<h3><a id="21_mapreduce_8"></a>2.1 map/reduce</h3>
<p>Python内建了<code>map()</code>和<code>reduce</code>函数。<br />
该函数的思想同Google发布的“MapReduce: Simplified Data Processing on Large Clusters”相同。</p>
<h5><a id="211_map_12"></a>2.1.1 map方法</h5>
<p><mark>eg. f(x)=x^2</mark></p>
<p>(1)用map方式实现</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(<span class="hljs-params">x</span>):
<span class="hljs-keyword">return</span> x * x
r = <span class="hljs-built_in">map</span>(f, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(r)) <span class="hljs-comment"># 输出:[1, 4, 9]</span>
</code></div></pre>
<p>(2)传统方式<br />
这种方法将 f(x)作用到了list的每一个元素,结果在新的list</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(<span class="hljs-params">x</span>):
<span class="hljs-keyword">return</span> x * x
L = []
<span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]:
L.append(f(n))
<span class="hljs-built_in">print</span>(L) <span class="hljs-comment"># 输出:[1, 4, 9]</span>
</code></div></pre>
<p><mark>eg. 将list所有数字转为字符串</mark></p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">str</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])))
<span class="hljs-comment"># 输出:['1', '2', '3']</span>
</code></div></pre>
<h5><a id="211_reduce_42"></a>2.1.1 reduce方法</h5>
<p><code>reduce</code> 把一个函数作用在以下序列 [x1, x2, x3,…] 上,这个函数必须介绍2个参数, <code>reduce</code> 把结果继续和序列的下一个元素做积累计算,其效果为</p>
<pre><div class="hljs"><code class="lang-python">reduce(f,[x1, x2, x3]) = f(f(f(f(x1, x2),x3),x4)
</code></div></pre>
<p><mark>序列求和</mark></p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 1 reduce方法</span>
<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> reduce
<span class="hljs-keyword">def</span> <span class="hljs-title function_">add</span>(<span class="hljs-params">x, y</span>):
<span class="hljs-built_in">print</span>(x, y)
<span class="hljs-keyword">return</span> x + y
<span class="hljs-comment"># 输出</span>
<span class="hljs-comment"># 1 2</span>
<span class="hljs-comment"># 3 3</span>
<span class="hljs-built_in">print</span>(reduce(add, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]))
<span class="hljs-comment"># 输出:6</span>
<span class="hljs-comment"># 2 直接sum方法</span>
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">sum</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]))
</code></div></pre>
<p><mark>把序列[1, 3, 5, 7, 9]变换成整数13579</mark></p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 把序列[1, 3, 5, 7, 9]变换成整数13579</span>
<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> reduce
<span class="hljs-keyword">def</span> <span class="hljs-title function_">fn</span>(<span class="hljs-params">x, y</span>):
<span class="hljs-keyword">return</span> x * <span class="hljs-number">10</span> + y
<span class="hljs-built_in">print</span>(reduce(fn, [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">9</span>]))
</code></div></pre>
<h3><a id="22_filter_78"></a>2.2 filter(筛选)</h3>
<p>Python内包含<code>filter()</code>函数用于过滤序列。<br />
与<code>map()</code>不同的是<code>filter()</code>把传入的函数依次作用于每个元素,然后根据返回值值<code>True</code>或<code>False</code>保留或删除元素。</p>
<p>filter返回的是一个Iterator, 是个惰性序列,所以要转为list</p>
<p>eg. 删除list中偶数,保留奇数</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">is_odd</span>(<span class="hljs-params">n</span>):
<span class="hljs-keyword">return</span> n % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">filter</span>(is_odd, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>])))
<span class="hljs-comment"># 输出:[1, 3, 5]</span>
</code></div></pre>
<p>eg. 删除序列中的空字符串</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">not_empty</span>(<span class="hljs-params">s</span>):
<span class="hljs-keyword">return</span> s <span class="hljs-keyword">and</span> s.strip()
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">filter</span>(not_empty, [<span class="hljs-string">'A'</span>, <span class="hljs-string">''</span>, <span class="hljs-string">'C'</span>, <span class="hljs-literal">None</span>, <span class="hljs-string">' '</span>])))
<span class="hljs-comment"># 输出:['A', 'C']</span>
</code></div></pre>
<h2><a id="3__lambda_103"></a>3 匿名函数 lambda</h2>
<p><code>lambda</code>:表示匿名函数,冒号前表示参数,冒号后为表达式(只能有一个)</p>
<p>eg. 求解f(x)=x^2</p>
<p>(1) <code>map()</code>函数</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(<span class="hljs-params">x</span>):
<span class="hljs-keyword">return</span> x * x
r = <span class="hljs-built_in">map</span>(f, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(r)) <span class="hljs-comment"># 输出:[1, 4, 9]</span>
</code></div></pre>
<p>(2)<code>lambda</code> 方法</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">map</span>(<span class="hljs-keyword">lambda</span> x: x * x, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])))
<span class="hljs-comment"># 输出:[1, 4, 9]</span>
</code></div></pre>
<h2><a id="4__124"></a>4 装饰器</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">use_logging</span>(<span class="hljs-params">func</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">wrapper</span>(<span class="hljs-params">*args, **kwargs</span>):
<span class="hljs-built_in">print</span>(<span class="hljs-string">">>>>>>>>>>>>>>>"</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">"%s 正在运行"</span> % func.__name__)
<span class="hljs-built_in">print</span>(*args)
<span class="hljs-built_in">print</span>(args[<span class="hljs-number">0</span>])
<span class="hljs-built_in">print</span>(args[<span class="hljs-number">1</span>])
<span class="hljs-built_in">print</span>(<span class="hljs-string">">>>>>>>>>>>>>>>"</span>)
<span class="hljs-keyword">return</span> func(*args)
<span class="hljs-keyword">return</span> wrapper
<span class="hljs-meta">@use_logging</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">getInfo</span>(<span class="hljs-params">name, age</span>):
<span class="hljs-built_in">print</span>(<span class="hljs-string">"-------"</span>)
<span class="hljs-built_in">print</span>(name, age)
getInfo(<span class="hljs-string">"有勇气的牛排"</span>, <span class="hljs-string">"20"</span>)
</code></div></pre>
留言