<legend id='3HFnQ'><style id='3HFnQ'><dir id='3HFnQ'><q id='3HFnQ'></q></dir></style></legend>

      <tfoot id='3HFnQ'></tfoot>

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

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

        如何在 numpy 中创建一个“接受"附加的空列表网格?

        How do I make a grid of empty lists in numpy that #39;accepts#39; appending?(如何在 numpy 中创建一个“接受附加的空列表网格?)
        • <bdo id='QGWAl'></bdo><ul id='QGWAl'></ul>

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

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

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

                  本文介绍了如何在 numpy 中创建一个“接受"附加的空列表网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am trying to use numpy.append but something goes wrong and it just doesn't make sence to me anymore. Can someone explain why I am getting an error?

                  >>> np.array([[], [], []]).shape
                  (3, 0)
                  
                  >>> a=[[], [], []]
                  >>> a[1].append(3)
                  >>> a
                  [[], [3], []]
                  
                  >>> b=np.array(a)
                  >>> b[0].append(3)
                  array([[3], [3], []], dtype=object)
                  

                  This is all logical to me, yet when I try the following it stops working.

                  >>> c=np.array((3,0),dtype=object)
                  >>> c[0].append(3)
                  AttributeError: 'int' object has no attribute 'append'
                  
                  ????
                  >>> np.empty((3,1))[0].append(3)
                  AttributeError: 'numpy.ndarray' object has no attribute 'append'
                  
                  >>> np.empty((3,0))[1].append(3)
                  AttributeError: 'numpy.ndarray' object has no attribute 'append'
                  
                  >>>np.empty((6,1),dtype=object)[0].append(3)
                  AttributeError: 'numpy.ndarray' object has no attribute 'append'
                  

                  Solved: How to create a numpy array of lists?

                  解决方案

                  Don't just look at the shape; check the dtype, and if object, the nature of the elements

                  In [282]: np.array([[], [], []])
                  Out[282]: array([], shape=(3, 0), dtype=float64)
                  

                  A 2d array of floats. np.array tries to make a multidimensional array of numbers; it's only when it can't do that it makes an object array.

                  In [283]: b=np.array([[],[3],[]])
                  In [284]: b
                  Out[284]: array([[], [3], []], dtype=object)
                  

                  Here the 3 sublists have different size, so it can't make a 2d array; the result is an object array, where the objects are lists, and have the append method.

                  In [286]: c=np.array((3,0), object)
                  In [287]: c
                  Out[287]: array([3, 0], dtype=object)
                  

                  This is a (2,) object array; the 2 elements are numbers. Numbers don't have an append method.

                  In [288]: np.empty((3,1))
                  Out[288]: 
                  array([[ 0.],
                         [ 0.],
                         [ 0.]])
                  

                  A (3,1) array of floats. No append method for numbers or arrays.

                  In [289]: np.empty((3,0))
                  Out[289]: array([], shape=(3, 0), dtype=float64)
                  

                  Another 2d array of floats

                  In [290]: np.empty((6,1),object)
                  Out[290]: 
                  array([[None],
                         [None],
                         [None],
                         [None],
                         [None],
                         [None]], dtype=object)
                  

                  2d array of dtype object. In this case they are initialized to None. Again no append method.

                  More on making an array of lists

                  dimensions of array of arrays in numpy

                  and

                  How to keep numpy from broadcasting when creating an object array of different shaped arrays


                  In [305]: d=np.empty((3,),object)
                  In [306]: d
                  Out[306]: array([None, None, None], dtype=object)
                  In [307]: d.fill([])
                  In [308]: d
                  Out[308]: array([[], [], []], dtype=object)   # array of lists
                  In [309]: d[0].append([1,2,3])
                  In [310]: d
                  Out[310]: array([[[1, 2, 3]], [[1, 2, 3]], [[1, 2, 3]]], dtype=object)
                  

                  But oops - those lists are all the same object (pointer) :( I have to put a different list in each element. Now I can append to them individually.

                  In [311]: d[...]=[[],[1,2,3],[2]]
                  In [312]: d
                  Out[312]: array([[], [1, 2, 3], [2]], dtype=object)
                  In [313]: d[0].append([2,3])
                  In [314]: d
                  Out[314]: array([[[2, 3]], [1, 2, 3], [2]], dtype=object)
                  

                  I think you have to bite the bullet and use a list to initialize an object array of lists. There isn't a short cut:

                  In [319]: d=np.empty((3,),object)
                  In [320]: d[...]=[[] for _ in range(3)]
                  In [321]: d
                  Out[321]: array([[], [], []], dtype=object)
                  In [323]: d
                  Out[323]: array([[], [3], []], dtype=object)
                  

                  这篇关于如何在 numpy 中创建一个“接受"附加的空列表网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    <tbody id='8TL47'></tbody>

                  1. <small id='8TL47'></small><noframes id='8TL47'>

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

                        1. <tfoot id='8TL47'></tfoot>