В качестве игрушечного примера я могу начать с двумерный массив ненулевых значений, но я не могу писать нули так, как ожидал.
Код: Выделить всё
import numpy as np
a = np.ones((6, 8))
indices = [2, 3, 5]
Код: Выделить всё
# I thought this would work, but it does not.
# It correctly writes to (2,2), (3,3), and (5,5), but not all
# combinations of (2, 3), (2, 5), (3, 2), (3, 5), (5, 2), or (5, 3)
a[indices, indices] = 0.0
print(a)
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 0. 1. 1. 1. 1. 1.]
[1. 1. 1. 0. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 0. 1. 1.]]
Код: Выделить всё
# I thought this would fix that problem, but it doesn't change the array.
a[indices, :][:, indices] = 0.0
print(a)
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]]
Код: Выделить всё
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 0. 0. 1. 0. 1. 1.]
[1. 1. 0. 0. 1. 0. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 0. 0. 1. 0. 1. 1.]]
Подробнее здесь: https://stackoverflow.com/questions/793 ... mn-indices
Мобильная версия