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

  • <small id='fNcNJ'></small><noframes id='fNcNJ'>

  • <tfoot id='fNcNJ'></tfoot>

        • <bdo id='fNcNJ'></bdo><ul id='fNcNJ'></ul>
        <legend id='fNcNJ'><style id='fNcNJ'><dir id='fNcNJ'><q id='fNcNJ'></q></dir></style></legend>

        追加到多维数组Python

        Appending to a multidimensional array Python(追加到多维数组Python)

        <small id='3wmx1'></small><noframes id='3wmx1'>

        • <bdo id='3wmx1'></bdo><ul id='3wmx1'></ul>

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

                <tfoot id='3wmx1'></tfoot>
                1. <legend id='3wmx1'><style id='3wmx1'><dir id='3wmx1'><q id='3wmx1'></q></dir></style></legend>
                  本文介绍了追加到多维数组Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在筛选数组ab以查找相同的值,然后我想将它们附加到一个新数组difference,但是我收到错误:ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 2。我如何能够修复此问题?

                  import numpy as np
                  a = np.array([[0,12],[1,40],[0,55],[1,23],[0,123.5],[1,4]])
                  b = np.array([[0,3],[1,10],[0,55],[1,34],[1,122],[0,123]])
                  difference= np.array([[]])
                  
                  
                  for i in a:
                      for j in b:
                          if np.allclose(i, j, atol=0.5):
                              difference = np.concatenate((difference,[i]))
                  

                  预期输出:

                  [[ 0. 55.],[  0.  123.5]]
                  

                  推荐答案

                  In [22]: a = np.array([[0,12],[1,40],[0,55],[1,23],[0,123.5],[1,4]])
                      ...: b = np.array([[0,3],[1,10],[0,55],[1,34],[1,122],[0,123]])
                  

                  使用直截了当的列表理解:

                  In [23]: [i for i in a for j in b if np.allclose(i,j,atol=0.5)]
                  Out[23]: [array([ 0., 55.]), array([  0. , 123.5])]
                  

                  但至于你方的连结。查看数组的形状:

                  In [24]: np.array([[]]).shape
                  Out[24]: (1, 0)
                  In [25]: np.array([i]).shape
                  Out[25]: (1, 1)
                  
                  这些只能在轴1上联接;默认值为0,这会给您带来错误。就像评论中写的那样,您必须了解数组形状才能使用concatenate

                  In [26]: difference= np.array([[]])
                      ...: for i in a:
                      ...:     for j in b:
                      ...:         if np.allclose(i, j, atol=0.5):
                      ...:             difference = np.concatenate((difference,[i]), axis=1)
                      ...: 
                  In [27]: difference
                  Out[27]: array([[  0. ,  55. ,   0. , 123.5]])
                  

                  矢量化

                  全数组方法:

                  Broadcaseab,生成(5,5,2)接近数组:

                  In [37]: np.isclose(a[:,None,:],b[None,:,:], atol=0.5)
                  Out[37]: 
                  array([[[ True, False],
                          [False, False],
                          [ True, False],
                          [False, False],
                          [False, False],
                          [ True, False]],
                  
                         [[False, False],
                          [ True, False],
                          [False, False],
                          [ True, False],
                          [ True, False],
                          [False, False]],
                  
                         [[ True, False],
                          [False, False],
                          [ True,  True],
                          [False, False],
                          [False, False],
                          [ True, False]],
                  
                         [[False, False],
                          [ True, False],
                          [False, False],
                          [ True, False],
                          [ True, False],
                          [False, False]],
                  
                         [[ True, False],
                          [False, False],
                          [ True, False],
                          [False, False],
                          [False, False],
                          [ True,  True]],
                  
                         [[False, False],
                          [ True, False],
                          [False, False],
                          [ True, False],
                          [ True, False],
                          [False, False]]])
                  

                  查找两列均为TRUE的位置,以及至少有一行&Quot;为:

                  的位置
                  In [38]: _.all(axis=2)
                  Out[38]: 
                  array([[False, False, False, False, False, False],
                         [False, False, False, False, False, False],
                         [False, False,  True, False, False, False],
                         [False, False, False, False, False, False],
                         [False, False, False, False, False,  True],
                         [False, False, False, False, False, False]])
                  In [39]: _.any(axis=1)
                  Out[39]: array([False, False,  True, False,  True, False])
                  In [40]: a[_]
                  Out[40]: 
                  array([[  0. ,  55. ],
                         [  0. , 123.5]])
                  

                  这篇关于追加到多维数组Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  GUI Freezes while downloading PyQt5 and Pytube(GUI在下载PyQt5和Pytube时冻结)
                  How to solve memory issues while multiprocessing using Pool.map()?(如何解决使用Pool.map()进行多处理时的内存问题?)
                  Python - How to use FastAPI and uvicorn.run without blocking the thread?(Python-如何使用FastAPI和uvicorn.run而不阻塞线程?)
                  How to increment a shared counter from multiple processes?(如何从多个进程递增共享计数器?)
                  Using pika, how to connect to rabbitmq running in docker, started with docker-compose with external network?(使用pika,如何连接运行在docker中的rabbitmq,从docker开始-与外部网络连接?)
                  How to use .rolling() on each row of a Pandas dataframe?(如何对 pandas 数据帧的每一行使用.roll()?)
                    1. <legend id='URx6K'><style id='URx6K'><dir id='URx6K'><q id='URx6K'></q></dir></style></legend><tfoot id='URx6K'></tfoot>

                        <tbody id='URx6K'></tbody>
                          <bdo id='URx6K'></bdo><ul id='URx6K'></ul>

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

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