numpy反向多维数组 [英] numpy reverse multidimensional array

查看:123
本文介绍了numpy反向多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在numpy中,最简单的方法是反转像这样的数组的最内部值:

What is the simplest way in numpy to reverse the most inner values of an array like this:

array([[[1, 1, 1, 2],
    [2, 2, 2, 3],
    [3, 3, 3, 4]],

   [[1, 1, 1, 2],
    [2, 2, 2, 3],
    [3, 3, 3, 4]]])

这样我得到以下结果:

array([[[2, 1, 1, 1],
    [3, 2, 2, 2],
    [4, 3, 3, 3]],

   [[2, 1, 1, 1],
    [3, 2, 2, 2],
    [4, 3, 3, 3]]])

非常感谢!

推荐答案

怎么样:

import numpy as np
a = np.array([[[10, 1, 1, 2],
               [2, 2, 2, 3],
               [3, 3, 3, 4]],
              [[1, 1, 1, 2],
               [2, 2, 2, 3],
               [3, 3, 3, 4]]])

,最后一个维度的反方向是:

and the reverse along the last dimension is:

b = a[:,:,::-1]

b = a[...,::-1]

尽管我更喜欢后者,因为前两个维度是隐式的,因此很难看到正在发生的事情.

although I like the later less since the first two dimensions are implicit and it is more difficult to see what is going on.

这篇关于numpy反向多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆