【发布时间】:2020-11-14 23:31:19
【问题描述】:
在 R 中我设法做到了,想法是在 Python 中做到,如果我在 Python 中应用 M[index],则顺序与 R 中的结果不同。
R 中的代码:
> M = matrix(c("a",0,"z",
+ 0,0,"b",
+ "c","y",0), nrow = 3, byrow = TRUE)
> M
[,1] [,2] [,3]
[1,] "a" "0" "z"
[2,] "0" "0" "b"
[3,] "c" "y" "0"
>
> index = c(3,2,1)
>
> M[index,index]
[,1] [,2] [,3]
[1,] "0" "y" "c"
[2,] "b" "0" "0"
[3,] "z" "0" "a"
>
Python 代码:
M = np.array([["a",0,"z"],
[0,0,"b"],
["c","y",0]])
index = [2,1,0]
print(M[index])
array([['c', 'y', '0'],
['0', '0', 'b'],
['a', '0', 'z']], dtype='<U1')
【问题讨论】:
-
尝试使用 OrderedDict,这里解释为 pymotw.com/2/collections/ordereddict.html
标签: python r python-3.x matrix