1. <tfoot id='xww7t'></tfoot>

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

        <bdo id='xww7t'></bdo><ul id='xww7t'></ul>

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

      <legend id='xww7t'><style id='xww7t'><dir id='xww7t'><q id='xww7t'></q></dir></style></legend>

    1. 未成功追加到空 NumPy 数组

      Unsuccessful append to an empty NumPy array(未成功追加到空 NumPy 数组)

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

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

              2. <tfoot id='zWzRf'></tfoot>
                本文介绍了未成功追加到空 NumPy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 append 用值填充一个空(不是 np.empty!)数组,但我遇到了错误:

                I am trying to fill an empty(not np.empty!) array with values using append but I am gettin error:

                我的代码如下:

                import numpy as np
                result=np.asarray([np.asarray([]),np.asarray([])])
                result[0]=np.append([result[0]],[1,2])
                

                我得到了:

                ValueError: could not broadcast input array from shape (2) into shape (0)
                

                推荐答案

                numpy.append 与 python 中的 list.append 有很大不同.我知道这让一些刚接触 numpy 的程序员望而却步.numpy.append 更像是连接,它创建一个新数组并用旧数组中的值和要附加的新值填充它.例如:

                numpy.append is pretty different from list.append in python. I know that's thrown off a few programers new to numpy. numpy.append is more like concatenate, it makes a new array and fills it with the values from the old array and the new value(s) to be appended. For example:

                import numpy
                
                old = numpy.array([1, 2, 3, 4])
                new = numpy.append(old, 5)
                print old
                # [1, 2, 3, 4]
                print new
                # [1, 2, 3, 4, 5]
                new = numpy.append(new, [6, 7])
                print new
                # [1, 2, 3, 4, 5, 6, 7]
                

                我认为您可以通过以下方式实现您的目标:

                I think you might be able to achieve your goal by doing something like:

                result = numpy.zeros((10,))
                result[0:2] = [1, 2]
                
                # Or
                result = numpy.zeros((10, 2))
                result[0, :] = [1, 2]
                

                更新:

                如果您需要使用循环创建一个 numpy 数组,并且您不提前知道数组的最终大小,您可以执行以下操作:

                If you need to create a numpy array using loop, and you don't know ahead of time what the final size of the array will be, you can do something like:

                import numpy as np
                
                a = np.array([0., 1.])
                b = np.array([2., 3.])
                
                temp = []
                while True:
                    rnd = random.randint(0, 100)
                    if rnd > 50:
                        temp.append(a)
                    else:
                        temp.append(b)
                    if rnd == 0:
                         break
                
                 result = np.array(temp)
                

                在我的示例中,结果将是一个 (N, 2) 数组,其中 N 是循环运行的次数,但显然您可以根据需要对其进行调整.

                In my example result will be an (N, 2) array, where N is the number of times the loop ran, but obviously you can adjust it to your needs.

                新的更新

                您看到的错误与类型无关,它与您尝试连接的 numpy 数组的形状有关.如果您执行 np.append(a, b),则 ab 的形状需要匹配.如果你附加一个 (2, n) 和 (n,) 你会得到一个 (3, n) 数组.您的代码正在尝试将 (1, 0) 附加到 (2,).这些形状不匹配,因此您会收到错误消息.

                The error you're seeing has nothing to do with types, it has to do with the shape of the numpy arrays you're trying to concatenate. If you do np.append(a, b) the shapes of a and b need to match. If you append an (2, n) and (n,) you'll get a (3, n) array. Your code is trying to append a (1, 0) to a (2,). Those shapes don't match so you get an error.

                这篇关于未成功追加到空 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 数据框制作线图?)
                  <bdo id='kyRhg'></bdo><ul id='kyRhg'></ul>

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

                    <tbody id='kyRhg'></tbody>

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