<small id='KBAcK'></small><noframes id='KBAcK'>

      • <bdo id='KBAcK'></bdo><ul id='KBAcK'></ul>
    1. <legend id='KBAcK'><style id='KBAcK'><dir id='KBAcK'><q id='KBAcK'></q></dir></style></legend>

      1. <i id='KBAcK'><tr id='KBAcK'><dt id='KBAcK'><q id='KBAcK'><span id='KBAcK'><b id='KBAcK'><form id='KBAcK'><ins id='KBAcK'></ins><ul id='KBAcK'></ul><sub id='KBAcK'></sub></form><legend id='KBAcK'></legend><bdo id='KBAcK'><pre id='KBAcK'><center id='KBAcK'></center></pre></bdo></b><th id='KBAcK'></th></span></q></dt></tr></i><div id='KBAcK'><tfoot id='KBAcK'></tfoot><dl id='KBAcK'><fieldset id='KBAcK'></fieldset></dl></div>
        <tfoot id='KBAcK'></tfoot>

        使用 nditer 进行浅迭代

        时间:2023-10-19
      2. <small id='fHT4x'></small><noframes id='fHT4x'>

          <tbody id='fHT4x'></tbody>
          <tfoot id='fHT4x'></tfoot>
          • <bdo id='fHT4x'></bdo><ul id='fHT4x'></ul>

              <legend id='fHT4x'><style id='fHT4x'><dir id='fHT4x'><q id='fHT4x'></q></dir></style></legend>
              <i id='fHT4x'><tr id='fHT4x'><dt id='fHT4x'><q id='fHT4x'><span id='fHT4x'><b id='fHT4x'><form id='fHT4x'><ins id='fHT4x'></ins><ul id='fHT4x'></ul><sub id='fHT4x'></sub></form><legend id='fHT4x'></legend><bdo id='fHT4x'><pre id='fHT4x'><center id='fHT4x'></center></pre></bdo></b><th id='fHT4x'></th></span></q></dt></tr></i><div id='fHT4x'><tfoot id='fHT4x'></tfoot><dl id='fHT4x'><fieldset id='fHT4x'></fieldset></dl></div>

                1. 本文介绍了使用 nditer 进行浅迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这样一个数组:

                  >>>y = np.random.randint(0, 255, (2,2,3))
                  >>>array([[[242, 14, 211],
                             [198,  7,   0]],
                  
                            [[235,  60,  81],
                             [164,  64, 236]]])
                  

                  而且我必须遍历每个 triplet 元素(不幸的是,矢量化对我没有帮助......).所以我尝试了:

                  And I have to iterate over each triplet element (unfortunately vectorization won't help me here...). So I tried:

                  for i, j in np.nditer(y):
                  print y[i, j],
                  

                  希望我能得到这样的输出:

                  hoping I'd get such an output:

                  [242, 14, 211], [198, 7, 0], [235, 60, 81], [164, 64, 236],但运气不好!

                  我得到错误:

                  Traceback (most recent call last):
                  
                    File "<ipython-input-21-a336ef837a8a>", line 1, in <module>
                      for i, j in np.nditer(y):    print y[i,j]
                  
                  TypeError: iteration over a 0-d array
                  

                  我很确定我犯了一个非常明显的错误...谁能帮帮我?

                  I'm quite sure I'm making a very obvious error... can anyone please help me?

                  推荐答案

                  看起来你只需要把它拉平.您可以使用 itertools 中的 chain 运算符.

                  It looks like you just need to flatten this down a level. You can use the chain operator from itertools.

                  from itertools import chain    
                  
                  
                  y = np.random.randint(0, 255, (2,2,3)
                  b = chain.from_iterable(y) # where b is a generator
                  

                  list(b) 输出

                  [array([ 51, 119,  84]),
                   array([ 50, 110, 193]),
                   array([165, 157,  52]),
                   array([239, 119,  83])]
                  

                  这篇关于使用 nditer 进行浅迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python:在迭代时将元素添加到列表中 下一篇:范围(len(list))还是枚举(list)?

                  相关文章

                    <legend id='29Uf8'><style id='29Uf8'><dir id='29Uf8'><q id='29Uf8'></q></dir></style></legend>
                    1. <tfoot id='29Uf8'></tfoot>
                      <i id='29Uf8'><tr id='29Uf8'><dt id='29Uf8'><q id='29Uf8'><span id='29Uf8'><b id='29Uf8'><form id='29Uf8'><ins id='29Uf8'></ins><ul id='29Uf8'></ul><sub id='29Uf8'></sub></form><legend id='29Uf8'></legend><bdo id='29Uf8'><pre id='29Uf8'><center id='29Uf8'></center></pre></bdo></b><th id='29Uf8'></th></span></q></dt></tr></i><div id='29Uf8'><tfoot id='29Uf8'></tfoot><dl id='29Uf8'><fieldset id='29Uf8'></fieldset></dl></div>

                      <small id='29Uf8'></small><noframes id='29Uf8'>

                      • <bdo id='29Uf8'></bdo><ul id='29Uf8'></ul>