[英]Set the last element equal to the first element in a multidimensional numpy array
For example I have an array: 例如我有一个数组:
[[[[1 2][3 4]]][[[1 2][3 4]]]]
How would I set 4 equal to 1? 我如何将4设置为1? I used
我用了
array[-1][-1][-1][-1] = array[0][0][0][0]
but I got an error because of it later on. 但是后来因为它我得到了一个错误。 Is there a more general way of doing this?
有更通用的方法吗?
You can "cheat" by updating the flattened array: 您可以通过更新展平的数组来“作弊”:
a = np.array([[[1,2],[3,4]],[[1,2],[3,4]]])
a.flat[-1] = a.flat[0]
a
array([[[1, 2],
[3, 4]],
[[1, 2],
[3, 1]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.