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

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

    1. <small id='vYMyF'></small><noframes id='vYMyF'>

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

        创建一个副本而不是 NumPy 数组的引用

        Create a copy and not a reference of a NumPy array(创建一个副本而不是 NumPy 数组的引用)

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

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

                  本文介绍了创建一个副本而不是 NumPy 数组的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 NumPy 制作 Python 程序,但遇到了问题:

                  I'm trying to make a Python program with NumPy, but I ran into a problem:

                  width, height, pngData, metaData = png.Reader(file).asDirect()
                  planeCount = metaData['planes']
                  print('Bildgroesse: ' + str(width) + 'x' + str(height) + ' Pixel')
                  image_2d = np.vstack(list(map(np.uint8, pngData)))
                  imageOriginal_3d = np.reshape(image_2d, (width, height, planeCount)) 
                  imageEdited_3d = imageOriginal_3d
                  

                  这是我的代码,用于读取 PNG 图像.现在我想编辑 imageEdited_3d 而不是 imageOriginal_3d,像这样:

                  This is my code, to read in a PNG image. Now I want to edit imageEdited_3d but NOT imageOriginal_3d, like this:

                  imageEdited_3d[x,y,0] = 255
                  

                  但是 imareOriginal_3d 变量的值与 imageEdited_3d 相同...

                  But then the imareOriginal_3d variable has the same values as the imageEdited_3d one...

                  有谁知道,我该如何解决这个问题?所以它不仅创建了一个引用,而且创建了一个真实的副本?:/

                  Does anyone know, how I can fix this? So it doesn't only creates a reference, but it creates a real copy? :/

                  推荐答案

                  你需要创建对象的副本.您可以使用 numpy.copy() 因为你有 numpy 对象.因此,您的初始化应该是这样的:

                  You need to create the copy of the object. You may do it using numpy.copy() since you are having numpy object. Hence, your initialisation should be like:

                  imageEdited_3d = imageOriginal_3d.copy()
                  

                  还有 copy 模块用于创建深拷贝浅拷贝.这与对象类型无关.例如,您使用 copy 的代码应为:

                  Also there is copy module for creating the deep copy OR, shallow copy. This works independent of object type. For example, your code using copy should be as:

                  from copy import copy, deepcopy
                  
                  # Creates shallow copy of object
                  imageEdited_3d = copy(imageOriginal_3d)
                  
                  # Creates deep copy of object
                  imageEdited_3d = deepcopy(imageOriginal_3d)
                  

                  说明:

                  浅拷贝构造一个新的复合对象,然后(到尽可能)将引用插入其中找到的对象原件.

                  A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

                  深拷贝构造一个新的复合对象,然后递归地,将原始对象中的对象的副本插入其中.

                  A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

                  这篇关于创建一个副本而不是 NumPy 数组的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 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 与否?)

                  • <bdo id='p1EGz'></bdo><ul id='p1EGz'></ul>

                          1. <legend id='p1EGz'><style id='p1EGz'><dir id='p1EGz'><q id='p1EGz'></q></dir></style></legend>

                            <tfoot id='p1EGz'></tfoot>

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

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