简体   繁体   English

将多维numpy数组中的最后一个元素设置为等于第一个元素

[英]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.

相关问题 如何将 numpy 数组拆分为 python 中的每个元素? 每个数组的最后一个元素将是另一个数组的第一个元素 - How can I split a numpy array into equal elements each in python? The last element of each array will be the first element of the other array NumPy 数组中的第一个和最后一个元素 - NumPy first and last element from array 创建列表的多维numpy数组,并为第一个元素依次分配一个数字 - Create multidimensional numpy array of lists and assign first element a number in sequence 获取多维numpy数组的第一个元素的pythonic方法 - pythonic way to get first element of multidimensional numpy array numpy数组:访问最后一个元素之后的第一个元素 - Numpy array: access first element after last element 查找大于阈值的NumPy数组的第一个和最后一个元素 - find first and last element of a NumPy array larger than a threshold 有没有一种有效的方法可以将 numpy 数组中的第一个元素移到最后一个元素? - Is there an efficient way to shift the first element to the last in a numpy array? numpy 数组中的第一个和最后一个元素在使用 genfromtxt 读取时作为 NaN - First and last element in numpy array comes as NaN while reading with genfromtxt 首先将元素添加到最后一个位置-Python numpy数组 - Add element in first an last position - Python numpy array 选择 numpy 数组,使得最后一个元素为 1 - Choosing numpy array such that last element is 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM