擅长:python、mysql、java
<p>可能是这样使用<code>numpy</code>:</p>
<pre><code>def shift(x, direction='up'):
if direction == 'up':
temp = range(x.shape[0])
indicies = temp[1:] + [temp[0]]
return x[indicies]
elif direction == 'left':
temp = range(x.shape[1])
indicies = temp[1:] + [temp[0]]
return x[:, indicies]
else:
print 'Error direction not known'
</code></pre>
<p>结果:</p>
<pre><code>>>> shift(l, direction='up')
array([[2, 5, 2],
[3, 3, 3],
[1, 1, 1]])
>>> shift(l, direction='left')
array([[1, 1, 1],
[5, 2, 2],
[3, 3, 3]])
>>> shift(l, direction='to the moon')
Error direction not known
</code></pre>