2016-11-28 106 views
3

镜像我有数字矩阵:Matrix在蟒蛇

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

,我想作相应的镜像:

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

然后再次产生:

[[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]] 

我想坚持像numpy这样的基本Python包。提前感谢任何帮助!

+0

有有很多方法可以做到这一点,你到目前为止做了什么? – Selcuk

+0

列表解析是你的朋友。 – Batman

回答

1

这可以在纯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])) 

显然,一旦建立镜像列表,你可以用它来创建一个numpy的阵列或任何...

0

这标记numpy所以我会假设你的矩阵是一个二维数组

In [937]: A=np.arange(9).reshape(3,3) 
In [938]: A 
Out[938]: 
array([[0, 1, 2], 
     [3, 4, 5], 
     [6, 7, 8]]) 

翻转它的行:

In [939]: A[::-1,:] 
Out[939]: 
array([[6, 7, 8], 
     [3, 4, 5], 
     [0, 1, 2]]) 

垂直串联

In [940]: np.concatenate((A[::-1,:],A), axis=0) 
Out[940]: 
array([[6, 7, 8], 
     [3, 4, 5], 
     [0, 1, 2], 
     [0, 1, 2], 
     [3, 4, 5], 
     [6, 7, 8]]) 

移除重复第一行

In [941]: np.concatenate((A[::-1,:],A[1:,:]), axis=0) 
Out[941]: 
array([[6, 7, 8], 
     [3, 4, 5], 
     [0, 1, 2], 
     [3, 4, 5], 
     [6, 7, 8]]) 

你认为你可以做一个水平(列)反转和连接(轴= 1)相同吗?

0

这里是一个非numpy的解决方案:

a = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']] 
b = list(reversed(a[1:])) + a # vertical mirror 
c = list(zip(*b)) # transpose 
d = list(reversed(c[1:])) + C# another vertical mirror 
e = list(zip(*d)) # transpose again 
0

假设你有

from numpy import array, concatenate 
m = array([[1, 2, 3], 
      [4, 5, 6], 
      [7, 8, 9]]) 

可以经由

>>> m[::-1, ...] 
array([[7, 8, 9], 
     [4, 5, 6], 
     [1, 2, 3]]) 

其中::-1从最后到第一的在步骤-1选择行沿第一(垂直)轴线翻转此。

要省略最后一排,明确要求选择到0之前立即停止:

>>> m[:0:-1, ...] 
array([[7, 8, 9], 
     [4, 5, 6]]) 

这可以然后沿着第一轴

p = concatenate([m[:0:-1, ...], m], axis=0) 

到形式串接

>>> p 
array([[7, 8, 9], 
     [4, 5, 6], 
     [1, 2, 3], 
     [4, 5, 6], 
     [7, 8, 9]]) 

这也可以沿另一个轴重复:

q = concatenate([p[..., :0:-1], p], axis=1) 

得到

>>> q 
array([[9, 8, 7, 8, 9], 
     [6, 5, 4, 5, 6], 
     [3, 2, 1, 2, 3], 
     [6, 5, 4, 5, 6], 
     [9, 8, 7, 8, 9]]) 
1
import numpy as np 

X= [[1, 2, 3], 
    [4, 5, 6], 
    [7, 8, 9]] 

A = np.asanyarray(X) 
B= np.flipud(A) 
C= np.concatenate((B, A[1:]), axis=0) 
D = C[:,1:] 
F = np.fliplr(C) 
E = np.concatenate((F, D), axis=1) 

print(E) 

我已经加入逐步转变。 flipudflipud refrence

输出

[[9 8 7 8 9] 
[6 5 4 5 6] 
[3 2 1 2 3] 
[6 5 4 5 6] 
[9 8 7 8 9]] 
0
m = [['a', 'b', 'c'], 
    ['d', 'e', 'f'], 
    ['g', 'h', 'i']] 

m_m = [[m[abs(i)][abs(j)] 
     for j in range(-len(m)+1, len(m))] 
     for i in range(-len(m)+1, len(m))] 

或者使用numpy的

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

m_m = m.T[meshgrid(*2*[abs(arange(-len(m) + 1, len(m)))])] 
1

使用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')