相关疑难解决方法(0)

迭代numpy.array的任意维度

是否有函数在numpy数组的任意维度上获取迭代器?

迭代第一维很容易......

In [63]: c = numpy.arange(24).reshape(2,3,4)

In [64]: for r in c :
   ....:     print r
   ....: 
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]
Run Code Online (Sandbox Code Playgroud)

但迭代其他维度更难.例如,最后一个维度:

In [73]: for r in c.swapaxes(2,0).swapaxes(1,2) :
   ....:     print r
   ....: 
[[ 0  4  8]
 [12 16 20]]
[[ 1  5  9]
 [13 17 21]]
[[ 2  6 10]
 [14 18 22]]
[[ 3 …
Run Code Online (Sandbox Code Playgroud)

python loops numpy

54
推荐指数
5
解决办法
4万
查看次数

标签 统计

loops ×1

numpy ×1

python ×1