【问题标题】:Iterating through a multidimensional array in a specific way以特定方式遍历多维数组
【发布时间】:2019-03-29 10:44:07
【问题描述】:

我有一个形状为(1000, 4, 200)的数组

import numpy as np 
array = np.ndarray((1000, 4, 200), dtype=int)

4用来表示通道数,200用来表示样本数,1000用来表示,嗯..时域(不知道怎么写)

我如何以特定的顺序遍历这个数组,这样当我打印数组的形状时,在 for 循环中,对于 200 个样本中的每一个,它都必须打印,

SAMPLE: 1 
(1000, ) #Channel1
(1000, ) #Channel2
(1000, ) #Channel3
(1000, ) #Channel4

SAMPLE: 2 
(1000, ) #Channel1
(1000, ) #Channel2
(1000, ) #Channel3
(1000, ) #Channel4

SAMPLE: 3 
(1000, ) #Channel1
(1000, ) #Channel2
(1000, ) #Channel3
(1000, ) #Channel4
.
.
.
SAMPLE: 200 
(1000, ) #Channel1
(1000, ) #Channel2
(1000, ) #Channel3
(1000, ) #Channel4

与其说我的打印语句应该准确,我只是希望能够以时域作为列向量一个接一个地提取每个通道..

我尝试了什么?

嗯.. 我没有做任何特别的事情,我只是有一个嵌套循环,从左到右遍历每个维度,这不是我想要的。

【问题讨论】:

    标签: python python-3.x loops numpy multidimensional-array


    【解决方案1】:

    实际上非常简单:只需使用转置..

    array = array.T
    for row in array:
        for channel in row:
            print(channel.shape)
        print()
    

    【讨论】:

      猜你喜欢
      • 2012-04-16
      • 2021-06-29
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      相关资源
      最近更新 更多