这里有一个或多或少的综合函数来解决它:
def shift(a, i, j, dir, n, fill=0, inplace=False):
out = a
if not inplace:
out = a.copy()
if dir == 'down':
if n < 0:
return shift(out, i, j, 'up', -n, fill=fill, inplace=True)
n = min(n, a.shape[0] - i)
out[i+n:, j] = a[i:a.shape[0] - n, j]
out[i:i+n, j] = fill
elif dir == 'up':
if n < 0:
return shift(out, i, j, 'down', -n, fill=fill, inplace=True)
n = min(n, i+1)
out[:i+1-n, j] = a[n:i+1, j]
out[i+1-n:i+1, j] = fill
elif dir == 'right':
if n < 0:
return shift(out, i, j, 'left', -n, fill=fill, inplace=True)
n = min(n, a.shape[1] - j)
out[i, j+n:] = a[i, j:a.shape[1] - n]
out[i, j:j+n] = fill
elif dir == 'left':
if n < 0:
return shift(out, i, j, 'right', -n, fill=fill, inplace=True)
n = min(n, j+1)
out[i, :j+1-n] = a[i, n:j+1]
out[i, j+1-n:j+1] = fill
else:
raise ValueError('Unknown direction "{}".'.format(dir))
return out
一些测试:
import numpy as np
arr = np.arange(25).reshape((5, 5))
print(arr)
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]]
print(shift(arr, 2, 1, 'up', 2))
# [[ 0 11 2 3 4]
# [ 5 0 7 8 9]
# [10 0 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]]
print(shift(arr, 2, 1, 'left', -2))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 0 0 11 12]
# [15 16 17 18 19]
# [20 21 22 23 24]]
print(shift(arr, 2, 1, 'down', 1, fill=100))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [ 10 100 12 13 14]
# [ 15 11 17 18 19]
# [ 20 16 22 23 24]]
shift(arr, 2, 1, 'right', 3, inplace=True)
print(arr)
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 0 0 0 11]
# [15 16 17 18 19]
# [20 21 22 23 24]]
编辑
根据 cmets 中的讨论,我添加了另一个函数来解决“滑动瓷砖”移动的问题:
import numpy as np
def shift_vector(v, i, n, empty=0):
if n < 0:
return shift_vector(v[::-1], len(v) - i - 1, -n)[::-1]
if n < len(v) - i:
# Find n empty places after i
idx = np.where(np.cumsum(v[i + 1:] == empty) == n)[0]
last_zero_idx = idx[0] if len(idx) > 0 else len(v) - i - 1
# Get non-empty values
v_slice = v[i + 1:i + last_zero_idx + 1]
values = v_slice[np.where(v_slice != empty)[0]]
# Copy to vector
v[i + n] = v[i]
r = range(i + n + 1, min(i + last_zero_idx + 2, len(v)))
v[r] = values[:len(r)]
v[i:i + n] = empty
return v
def shift(a, i, j, dir, n, empty=0, inplace=False):
out = a
if not inplace:
out = a.copy()
if dir == 'down':
out[:, j] = shift_vector(out[:, j], i, n, empty=empty)
elif dir == 'up':
out[:, j] = shift_vector(out[:, j], i, -n, empty=empty)
elif dir == 'right':
out[i, :] = shift_vector(out[i, :], j, n, empty=empty)
elif dir == 'left':
out[i, :] = shift_vector(out[i, :], j, -n, empty=empty)
else:
raise ValueError('Unknown direction "{}".'.format(dir))
return out
m = np.array([[1, 0, 0, 2],
[3, 4, 0, 0],
[5, 0, 6, 7],
[0, 8, 9, 0]])
print("m")
print(m)
print("shift(m, 1, 0, 'right', 2)")
print(shift(m, 1, 0, 'right', 2))
print("shift(m, 3, 1, 'down', -2)")
print(shift(m, 3, 1, 'down', -2))
print("shift(m, 0, 3, 'left', 3)")
print(shift(m, 0, 3, 'left', 3))
print("shift(m, 2, 2, 'up', 1)")
print(shift(m, 2, 2, 'up', 1))
输出:
m
[[1 0 0 2]
[3 4 0 0]
[5 0 6 7]
[0 8 9 0]]
shift(m, 1, 0, 'right', 2)
[[1 0 0 2]
[0 0 3 4]
[5 0 6 7]
[0 8 9 0]]
shift(m, 3, 1, 'down', -2)
[[1 4 0 2]
[3 8 0 0]
[5 0 6 7]
[0 0 9 0]]
shift(m, 0, 3, 'left', 3)
[[2 0 0 0]
[3 4 0 0]
[5 0 6 7]
[0 8 9 0]]
shift(m, 2, 2, 'up', 1)
[[1 0 0 2]
[3 4 6 0]
[5 0 0 7]
[0 8 9 0]]