简体   繁体   English

如何使用ndenumerate索引numpy数组?

[英]How to index numpy array using ndenumerate?

The following is part of some code. 以下是一些代码的一部分。

import numpy as np
nnet.temp = np.zeros(nnet.max_layer_size, dtype=np.float64)
##some code
nnet.temp[i] = temp_val
for index, x in np.ndenumerate(nnet.temp):
   print(index, x)

here, when i try to print as "print (nnet.temp)", it prints as follows. 在这里,当我尝试打印为“ print(nnet.temp)”时,其打印如下。

[0.12233607 0.         0.19732719 0.         0.23104049 0.
 0.         0.         0.         0.         0.         0.19245713]
[0.         2.48127313 0.         0.         0.         0.21570966
 0.         0.         0.39054759 0.         0.         0.        ]

As i want to print them with index, i tried using "ndenumerate" as follows 因为我想用索引打印它们,所以我尝试使用“ ndenumerate”,如下所示

for index, x in np.ndenumerate(nnet.temp):
   print(index, x)
(0,) 0.12233607087416862
(1,) 0.0
(2,) 0.19732719295877543
(3,) 0.0
(4,) 0.23104048678483236
(5,) 0.0
(6,) 0.0
(7,) 0.0
(8,) 0.0
(9,) 0.0
(10,) 0.0
(11,) 0.19245712897455036
(0,) 0.0
(1,) 2.4812731271543647
(2,) 0.0
(3,) 0.0
(4,) 0.0
(5,) 0.21570965947566847
(6,) 0.0
(7,) 0.0
(8,) 0.39054758850346893
(9,) 0.0
(10,) 0.0
(11,) 0.0

whereas i want to print them as follows. 而我想按如下方式打印它们。

(0,0) 0.12233607087416862
(0,1) 0.0
(0,2) 0.19732719295877543
(0,3) 0.0
(0,4) 0.23104048678483236
(0,5) 0.0
(0,6) 0.0
(0,7) 0.0
(0,8) 0.0
(0,9) 0.0
(0,10) 0.0
(0,11) 0.19245712897455036
(1,0) 0.0
(1,1) 2.4812731271543647
(1,2) 0.0
(1,3) 0.0
(1,4) 0.0
(1,5) 0.21570965947566847
(1,6) 0.0
(1,7) 0.0
(1,8) 0.39054758850346893
(1,9) 0.0
(1,10) 0.0
(1,11) 0.0

what is the mistake here, any suggestion will be highly helpful. 这是什么错误,任何建议都将非常有帮助。 Thanks in advance. 提前致谢。

It works for me: 这个对我有用:

In [146]: for idx,x in np.ndenumerate(np.zeros((3,2))):print(idx,x)                                          
(0, 0) 0.0
(0, 1) 0.0
(1, 0) 0.0
(1, 1) 0.0
(2, 0) 0.0
(2, 1) 0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM