Python中的矩阵镜像

Ste*_*ers 5 python arrays numpy matrix python-3.x

我有一个数字矩阵:

[[a, b, c] 
 [d, e, f] 
 [g, h, i]]
Run Code Online (Sandbox Code Playgroud)

我想据此进行镜像:

[[g, h, i]
 [d, e, f]
 [a, b, c] 
 [d, e, f] 
 [g, h, i]]
Run Code Online (Sandbox Code Playgroud)

然后再次产生:

[[i, h, g, h, i]
 [f, e, d, e, f]
 [c, b, a, b, c] 
 [f, e, d, e, f] 
 [i, h, g, h, i]]
Run Code Online (Sandbox Code Playgroud)

我想坚持使用像numpy这样的基本Python包。在此先感谢您的帮助!

Dan*_*l F 6

numpy.lib.pad与使用'reflect'

m = [['a', 'b', 'c'], 
     ['d', 'e', 'f'], 
     ['g', 'h', 'i']]

n=np.lib.pad(m,((2,0),(2,0)),'reflect')

n
Out[8]: 
array([['i', 'h', 'g', 'h', 'i'],
       ['f', 'e', 'd', 'e', 'f'],
       ['c', 'b', 'a', 'b', 'c'],
       ['f', 'e', 'd', 'e', 'f'],
       ['i', 'h', 'g', 'h', 'i']], 
      dtype='<U1')
Run Code Online (Sandbox Code Playgroud)


mgi*_*son 5

这可以通过在纯python中使用简单的辅助函数来完成:

def mirror(seq):
    output = list(seq[::-1])
    output.extend(seq[1:])
    return output

inputs = [
   ['a', 'b', 'c'],
   ['d', 'e', 'f'],
   ['g', 'h', 'i'],
]
print(mirror([mirror(sublist) for sublist in inputs]))
Run Code Online (Sandbox Code Playgroud)

显然,一旦创建了镜像列表,您就可以使用它创建一个numpy数组或其他任何东西。