赋不同值
import numpy as np
data = score = np.array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86]])
row, col = np.diag_indices_from(data)
data[row,col] = np.array([1, 2, 3, 4, 5])
print(data)
[[ 1 89 86 67 79]
[78 2 89 67 81]
[90 94 3 67 74]
[91 91 90 4 69]
[76 87 75 67 5]]
赋相同值
import numpy as np
data = score = np.array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86]])
row, col = np.diag_indices_from(data)
data[row,col] = 100
print(data)
[[100 89 86 67 79]
[ 78 100 89 67 81]
[ 90 94 100 67 74]
[ 91 91 90 100 69]
[ 76 87 75 67 100]]
转载请注明原文地址:https://ipadbbs.8miu.com/read-25731.html