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

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

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

        将一个 numpy 数组附加到一个列表 - 奇怪的事情

        时间:2023-09-27

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

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

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

                  <legend id='RHUin'><style id='RHUin'><dir id='RHUin'><q id='RHUin'></q></dir></style></legend>
                1. 本文介绍了将一个 numpy 数组附加到一个列表 - 奇怪的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Raspberry Pi 上的 Raspbian 上使用 Spyder 3.1.3 中的 Python3.5.3.将两个 numpy-arrays 附加到名为list0"的列表中可以很好地与分配的 numpy 数组a"类似:

                  Using Python3.5.3 in Spyder 3.1.3 on Raspbian on a Raspberry Pi. Appending two numpy-arrays to a list named 'list0' works well with allocated numpy arrays 'a' like:

                  import numpy as np
                  
                  list0 = []
                  a = np.array([[1,2,3],[2,3,4]])
                  list0.append(a)
                  a = np.array([[11,12,13],[12,13,14]])
                  list0.append(a)
                  
                  print("list0 =",list0)
                  

                  效果很好,作为输出提供(帖子的格式更好):

                  works well, giving as output (a bit better formatted for the post):

                  list0 = [ array([[ 1,  2,  3], [ 2,  3,  4]]), 
                            array([[11, 12, 13], [12, 13, 14]]) ]
                  

                  使用循环将赋值替换为a,奇怪的事情发生了:

                  Replacing the assignment to a using a loop, weird things happen:

                  import numpy as np
                  a = np.empty((3), int)
                  list0 = []
                  for idx in range(4):    
                      for i in range(3):
                          a[i] = idx*10 + i
                      print("idx =",idx,"; a =",a)
                      list0.append(a)
                  print("list0 =",list0)
                  

                  第二行告诉 Python 使用的数组的形状(在我原来的例子中,它是一个三维数组).为了验证生成的名为a"的数组被打印出来.将新填充的数组 'a' 附加到 'list0' 最终显示最后一行的四倍.

                  The second line tells Python the shape of the array used (in my original case it is a three-dimensional array). For verification the generated arrays named 'a' are printed out. Appending the newly filled arrays 'a' to 'list0' finally shows four times the last line.

                  idx = 0 ; a = [ 0  1  2]
                  idx = 1 ; a = [10 11 12]
                  idx = 2 ; a = [20 21 22]
                  idx = 3 ; a = [30 31 32]
                  list0 = [ array([30, 31, 32]), array([30, 31, 32]), 
                            array([30, 31, 32]), array([30, 31, 32]) ] 
                  

                  我想list0"只包含四倍的指向数组a"的指针,该数组只存在于一个实例/内存范围中.

                  I suppose that 'list0' simply contains four times a pointer to the array 'a' which only exists in one instance / memory range.

                  那么:如何将每个不同的数组 'a' 物理附加(复制?)到列表中?它是一个 python 错误还是只是我对某些东西的误解?当然,我应该多想pythonian ;c)

                  So: How can I physically append (copy?) each of the different arrays 'a' to the list? Is it a python bug or is it simply my misunderstanding of something? Certainly I should think more pythonian ;c)

                  谢谢你的帮助,彼得

                  推荐答案

                  问题

                  您将相同的数组 a 附加到您的 list0 4 次.像 a 这样的数组是可变对象,这意味着,当你给它们赋值时,底层对象会发生变化.由于该数组在您的列表中出现了 4 次,因此这些更改(似乎)出现在 4 个不同的地方.

                  The problem

                  You're appending the same array a to your list0 4 times. Arrays like a are mutable objects, which means, among other things, that when you assign values to them the underlying object changes. Since the array is present in your list 4 times, those changes (seem to) show up in 4 different places.

                  您只需稍作改动即可修复您拥有的代码.将数组的副本附加到列表中,而不是数组本身:

                  You can fix the code you have with one small change. Append a copy of the array to your list, instead of the array itself:

                  import numpy as np
                  a = np.empty((3), int)
                  list0 = []
                  for idx in range(4):    
                      for i in range(3):
                          a[i] = idx*10 + i
                      print("idx =",idx,"; a =",a)
                      list0.append(a.copy())
                  print("list0 =",list0)
                  

                  输出:

                  idx = 0 ; a = [0 1 2]
                  idx = 1 ; a = [10 11 12]
                  idx = 2 ; a = [20 21 22]
                  idx = 3 ; a = [30 31 32]
                  list0 = [array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]
                  

                  优化方案

                  Python/Numpy 提供了许多更好的方法(无论是使用更少的代码行还是运行速度更快)来初始化数组.对于像这样的一堆范围,这是一个合理的方法:

                  Optimized solution

                  Python/Numpy offer many better ways (both in terms of using fewer lines of code and running faster) to initialize arrays. For a bunch of ranges like this, here is a reasonable approach:

                  list0 = [np.arange(n*10, n*10+3) for n in range(4)]
                  print(list0)
                  

                  输出:

                  [array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]
                  

                  您也可以考虑只使用单个二维数组来代替数组列表.单个数组通常比列表中的异构数组更易于使用.这样做的方法如下:

                  You might also consider just using a single 2D array in place of a list of arrays. One single array is often easier to work with than a heterogenous mix of arrays in a list. Here's how you do that:

                  arr0 = np.array([np.arange(n*10, n*10+3) for n in range(4)])
                  print(arr0)
                  

                  输出:

                  [[ 0  1  2]
                   [10 11 12]
                   [20 21 22]
                   [30 31 32]]
                  

                  这篇关于将一个 numpy 数组附加到一个列表 - 奇怪的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:比较Python中连续元组列表的第一个元素 下一篇:在维护列数据类型的同时将行插入 pandas DataFrame

                  相关文章

                  <tfoot id='Yeib5'></tfoot>

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

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

                    3. <legend id='Yeib5'><style id='Yeib5'><dir id='Yeib5'><q id='Yeib5'></q></dir></style></legend>