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

      <legend id='nGLwk'><style id='nGLwk'><dir id='nGLwk'><q id='nGLwk'></q></dir></style></legend>
    1. <small id='nGLwk'></small><noframes id='nGLwk'>

      初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal()

      Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
          <tbody id='rNh7j'></tbody>
        <i id='rNh7j'><tr id='rNh7j'><dt id='rNh7j'><q id='rNh7j'><span id='rNh7j'><b id='rNh7j'><form id='rNh7j'><ins id='rNh7j'></ins><ul id='rNh7j'></ul><sub id='rNh7j'></sub></form><legend id='rNh7j'></legend><bdo id='rNh7j'><pre id='rNh7j'><center id='rNh7j'></center></pre></bdo></b><th id='rNh7j'></th></span></q></dt></tr></i><div id='rNh7j'><tfoot id='rNh7j'></tfoot><dl id='rNh7j'><fieldset id='rNh7j'></fieldset></dl></div>
      • <legend id='rNh7j'><style id='rNh7j'><dir id='rNh7j'><q id='rNh7j'></q></dir></style></legend>

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

          • <bdo id='rNh7j'></bdo><ul id='rNh7j'></ul>
            • <tfoot id='rNh7j'></tfoot>

              1. 本文介绍了初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我找不到任何描述如何执行此操作的内容,这导致我相信我没有以正确的惯用 Python 方式执行此操作.也将不胜感激有关正确" Python 方法的建议.

                I was unable to find anything describing how to do this, which leads to be believe I'm not doing this in the proper idiomatic Python way. Advice on the 'proper' Python way to do this would also be appreciated.

                我正在编写的数据记录器有一堆变量(任意记录长度,最大长度已知).在 MATLAB 中,我会将它们全部初始化为长度为 n 的一维零数组,n 大于我见过的条目数,在记录循环,并在测量结束时修剪掉多余的零.初始化如下所示:

                I have a bunch of variables for a datalogger I'm writing (arbitrary logging length, with a known maximum length). In MATLAB, I would initialize them all as 1-D arrays of zeros of length n, n bigger than the number of entries I would ever see, assign each individual element variable(measurement_no) = data_point in the logging loop, and trim off the extraneous zeros when the measurement was over. The initialization would look like this:

                [dData gData cTotalEnergy cResFinal etc] = deal(zeros(n,1));
                

                有没有办法在 Python/NumPy 中做到这一点,所以我不必将每个变量放在自己的行上:

                Is there a way to do this in Python/NumPy so I don't either have to put each variable on its own line:

                dData = np.zeros(n)
                gData = np.zeros(n)
                etc.
                

                我也不想只制作一个大矩阵,因为跟踪哪一列是哪个变量是不愉快的.也许解决方案是制作 (length x numvars) 矩阵,并将列切片分配给各个变量?

                I would also prefer not just make one big matrix, because keeping track of which column is which variable is unpleasant. Perhaps the solution is to make the (length x numvars) matrix, and assign the column slices out to individual variables?

                假设到这结束时我将有很多相同长度的向量;例如,我的后处理获取每个日志文件,计算一堆单独的指标(>50),存储它们,然后重复,直到所有日志都被处理完.然后我生成直方图,means/maxes/sigmas/etc.对于我计算的所有各种指标.由于在 Python 中初始化 50 多个向量显然并不容易,那么最好的(最干净的代码和良好的性能)方法是什么?

                Assume I'm going to have a lot of vectors of the same length by the time this is over; e.g., my post-processing takes each log file, calculates a bunch of separate metrics (>50), stores them, and repeats until the logs are all processed. Then I generate histograms, means/maxes/sigmas/etc. for all the various metrics I computed. Since initializing 50+ vectors is clearly not easy in Python, what's the best (cleanest code and decent performance) way of doing this?

                推荐答案

                如果你真的有动力在单行中做到这一点,你可以创建一个 (n_vars, ...) 数组零,然后沿第一维解包:

                If you're really motivated to do this in a one-liner you could create an (n_vars, ...) array of zeros, then unpack it along the first dimension:

                a, b, c = np.zeros((3, 5))
                print(a is b)
                # False
                

                另一种选择是使用列表推导式或生成器表达式:

                Another option is to use a list comprehension or a generator expression:

                a, b, c = [np.zeros(5) for _ in range(3)]   # list comprehension
                d, e, f = (np.zeros(5) for _ in range(3))   # generator expression
                print(a is b, d is e)
                # False False
                

                不过要小心!您可能认为在包含您对 np.zeros() 的调用的列表或元组上使用 * 运算符可以达到同样的效果,但事实并非如此:

                Be careful, though! You might think that using the * operator on a list or tuple containing your call to np.zeros() would achieve the same thing, but it doesn't:

                h, i, j = (np.zeros(5),) * 3
                print(h is i)
                # True
                

                这是因为元组内的表达式首先被求值.np.zeros(5) 因此只被调用一次,重复元组中的每个元素最终都是对同一个数组的引用.这就是你不能只使用 a = b = c = np.zeros(5) 的原因.

                This is because the expression inside the tuple gets evaluated first. np.zeros(5) therefore only gets called once, and each element in the repeated tuple ends up being a reference to the same array. This is the same reason why you can't just use a = b = c = np.zeros(5).

                除非您确实需要分配大量空数组变量并且您非常关心使代码紧凑(!),否则我建议您在单独的行中初始化它们以提高可读性.

                Unless you really need to assign a large number of empty array variables and you really care deeply about making your code compact (!), I would recommend initialising them on separate lines for readability.

                这篇关于初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                Python - Initializing Multiple Lists/Line(Python - 初始化多个列表/行)

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

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

                            <tbody id='qEcbh'></tbody>
                          <tfoot id='qEcbh'></tfoot>