我有一个数字矩阵:
[[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 :(得分:4)
这可以使用纯python中的简单辅助函数来完成:
select q1.hvsrChildID as CID, q1.hvsrParentID as PID, q1.avg as avg1, q2.avg as avg2,
a3.avg as avg3, q4.avg as avg4 from (select hvsrChildID, hvsrParentID,
avg(hvsrDaysParentReadWithChild) as AVG from tblHVSR where hvsrChildID in
((select rosterChildID from tblRoster where rosterChildActiveYN = 1)) and
hvsrDateOfContact BETWEEN curdate() - interval 1 month AND curdate() group by hvsrChildID)
as q1 LEFT JOIN (select hvsrChildID, hvsrParentID, avg(hvsrDaysParentReadWithChild) as AVG
from tblHVSR where hvsrChildID in ((select rosterChildID from tblRoster where
rosterChildActiveYN = 1)) and hvsrDateOfContact BETWEEN curdate() - interval 3 month AND
curdate() group by hvsrChildID) as q2 LEFT JOIN (select hvsrChildID, hvsrParentID,
avg(hvsrDaysParentReadWithChild) as AVG from tblHVSR where hvsrChildID in
((select rosterChildID from tblRoster where rosterChildActiveYN = 1)) and
hvsrDateOfContact BETWEEN curdate() - interval 6 month AND curdate() group by hvsrChildID)
as q3 LEFT JOIN (select hvsrChildID, hvsrParentID, avg(hvsrDaysParentReadWithChild) as AVG
from tblHVSR where hvsrChildID in ((select rosterChildID from tblRoster where
rosterChildActiveYN = 1)) and hvsrDateOfContact BETWEEN curdate() - interval 1 year AND
curdate() group by hvsrChildID) as q4;
SELECT * FROM (select hvsrChildID, hvsrParentID, avg(hvsrDaysParentReadWithChild) as AVG1
from tblHVSR where hvsrChildID in (select rosterChildID from tblRoster where
rosterChildActiveYN = 1) and hvsrDateOfContact BETWEEN curdate() - interval 1 month
AND curdate() group by hvsrChildID) A, (select hvsrChildID, hvsrParentID,
avg(hvsrDaysParentReadWithChild) as AVG2 from tblHVSR where hvsrChildID
in (select rosterChildID from tblRoster where rosterChildActiveYN = 1) and
hvsrDateOfContact BETWEEN curdate() - interval 3 month AND curdate() group by hvsrChildID)
B, (select hvsrChildID, hvsrParentID, avg(hvsrDaysParentReadWithChild) as AVG3 from
tblHVSR where hvsrChildID in (select rosterChildID from tblRoster where
rosterChildActiveYN = 1) and hvsrDateOfContact BETWEEN curdate() - interval 6 month AND
curdate() group by hvsrChildID) C, (select hvsrChildID, hvsrParentID,
avg(hvsrDaysParentReadWithChild) as AVG4 from tblHVSR where hvsrChildID
in (select rosterChildID from tblRoster where rosterChildActiveYN = 1) and
hvsrDateOfContact BETWEEN curdate() - interval 1 year AND curdate() group by hvsrChildID) D;
select a.childID, b.parentID, concat_ws(' ', a.childFName, a.childLName) as CNAME,
concat_ws(' ', b.parentFName, b.parentLName) as PNAME, (select
avg(d.hvsrDaysParentReadWithChild) as avg1 from tblHVSR d where d.hvsrChildID = a.childID
and hvsrDateOfContact BETWEEN curdate() - interval 1 month AND curdate()) as AVG1 from
tblChildren a, tblParents b where a.childID in (select c.rosterChildID from tblRoster c
where c.rosterChildActiveYN = '1') and b.parentID in (select c.rosterParentID from
tblRoster c where c.rosterChildActiveYN = '1' and c.rosterParentActiveYN = '1') and
a.childParentID = b.parentID;
显然,一旦创建了镜像列表,你就可以用它来创建一个numpy数组或其他......
答案 1 :(得分:3)
将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')
答案 2 :(得分:2)
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)
输出
[[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]]
答案 3 :(得分:1)
假设你有
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]])
答案 4 :(得分: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)来做同样的事情吗?
答案 5 :(得分:0)
这是一个非笨拙的解决方案:
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
答案 6 :(得分: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)))])]