擅长:python、mysql、java
<h2>编写自己的求和函数</h2>
<p>模块<code>functools</code>有一个有用的<a href="http://book.pythontips.com/en/latest/map_filter.html" rel="nofollow noreferrer">^{<cd2>}</a>,可以用来编写自己的sum函数。如果你对<a href="http://www.secnetix.de/olli/Python/lambda_functions.hawk" rel="nofollow noreferrer">^{<cd3>}</a>很满意,你可以这样做:</p>
<pre><code>lst = [0,1,2,3,4,5]
</code></pre>
<p>也就是说<code>sum(lst)</code>就是<code>15</code>。但是,您自己使用<code>reduce</code>的sum函数可能类似于:</p>
<pre><code>from functools import reduce
reduce(lambda x,y: x + y, l)
</code></pre>
<p>它也会给出<code>15</code>。你应该可以自己写剩下的(即在另一个处理行的列表中)。</p>