擅长:python、mysql、java
<p>一个更简单的解决方案是使用<code>zip</code>将列表列表转置</em>并在每个新的子列表中使用<code>sum</code>:</p>
<pre><code>def column_sums(square):
return [sum(i) for i in zip(*square)]
</code></pre>
<p><code>zip(*square)</code><em>解压缩列表列表,并连续返回每个列中的所有项(<em>压缩的</em>为元组)。在</p>
^{pr2}$
<hr/>
<p>您也可以使用<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html" rel="nofollow">^{<cd4>}</a>来实现这一点,方法是将<code>axis</code>参数设置为<code>0</code>,意思是沿行求和(即每列求和):</p>
<pre><code>>>> import numpy as np
>>> square = np.array(square)
>>> square.sum(axis=0)
array([28, 32, 36, 40])
</code></pre>