📜  Python中的数组复制

📅  最后修改于: 2022-05-13 01:55:16.054000             🧑  作者: Mango

Python中的数组复制

让我们看看如何在Python中复制数组。有 3 种复制数组的方法:

  • 只需使用赋值运算符。
  • 浅拷贝
  • 深拷贝

分配数组

我们可以使用赋值运算符(=) 创建数组的副本。

句法 :

new_arr = old_ arr

在Python中,赋值语句不会复制对象,它们会在目标和对象之间创建绑定。当我们使用 =运算符时,用户认为这会创建一个新对象;好吧,它没有。它只创建一个共享原始对象引用的新变量。

例子:

Python3
# importing the module
from numpy import *                  
 
# creating the first array
arr1 = array([2, 6, 9, 4])            
 
# displaying the identity of arr1
print(id(arr1))
 
# assigning arr1 to arr2
arr2 = arr1                         
 
# displaying the identity of arr2
print(id(arr2))
 
# making a change in arr1
arr1[1] = 7                        
 
# displaying the arrays
print(arr1)
print(arr2)


Python3
# importing the module
from numpy import *                 
  
# creating the first array
arr1 = array([2, 6, 9, 4])
 
# displaying the identity of arr1
print(id(arr1))
 
# shallow copy arr1 in arr2 using view()
arr2 = arr1.view() 
 
# displaying the identity of arr2
print(id(arr2))
  
# making a change in arr1
arr1[1] = 7                       
  
# displaying the arrays
print(arr1)
print(arr2)


Python3
# importing the module
from numpy import *                 
  
# creating the first array
arr1 = array([2, 6, 9, 4])
 
# displaying the identity of arr1
print(id(arr1))
 
# shallow copy arr1 in arr2 using view()
arr2 = arr1.copy()
 
# displaying the identity of arr2
print(id(arr2))
  
# making a change in arr1
arr1[1] = 7                       
  
# displaying the arrays
print(arr1)
print(arr2)


Python3
import copy
 
 
def rotate_matrix(image):
    # Copy method one
    copy_image_one = copy.deepcopy(image)
    print("Original", matrix)
    print("Copy of original", copy_image_one)
    N = len(matrix)
 
    # Part 1, reverse order within each row
    for row in range(N):
        for column in range(N):
            copy_image_one[row][column] = image[row][N-column-1]
 
    print("After modification")
    print("Original", matrix)
    print("Copy", copy_image_one)
 
    # Copy method two
    copy_image_two = [list(row) for row in copy_image_one]
    # Test on what happens when you remove list from the above code.
 
    # Part 2, transpose
    for row in range(N):
        for column in range(N):
            copy_image_two[column][row] = copy_image_one[row][column]
 
    return copy_image_two
 
 
if __name__ == "__main__":
    matrix = [[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]]
    print("Rotated image", rotate_matrix(matrix))


输出 :

117854800
117854800
[2 7 9 4]
[2 7 9 4]

我们可以看到两个数组都引用了同一个对象。

浅拷贝

浅拷贝意味着构造一个新的集合对象,然后用在原始集合中找到的子对象的引用来填充它。复制过程不会递归,因此不会创建子对象本身的副本。在浅拷贝的情况下,对象的引用被复制到另一个对象中。这意味着对对象副本所做的任何更改都会反映在原始对象中。我们将使用view()函数实现浅拷贝。

例子 :

Python3

# importing the module
from numpy import *                 
  
# creating the first array
arr1 = array([2, 6, 9, 4])
 
# displaying the identity of arr1
print(id(arr1))
 
# shallow copy arr1 in arr2 using view()
arr2 = arr1.view() 
 
# displaying the identity of arr2
print(id(arr2))
  
# making a change in arr1
arr1[1] = 7                       
  
# displaying the arrays
print(arr1)
print(arr2)

这一次虽然 2 个数组引用了不同的对象,但仍然在改变一个的值时,另一个的值也发生了变化。

深拷贝

深拷贝是复制过程递归发生的过程。这意味着首先构造一个新的集合对象,然后递归地用在原始集合中找到的子对象的副本填充它。在深拷贝的情况下,对象的副本被复制到另一个对象中。这意味着对对象副本所做的任何更改都不会反映在原始对象中。我们将使用copy()函数实现深拷贝。

Python3

# importing the module
from numpy import *                 
  
# creating the first array
arr1 = array([2, 6, 9, 4])
 
# displaying the identity of arr1
print(id(arr1))
 
# shallow copy arr1 in arr2 using view()
arr2 = arr1.copy()
 
# displaying the identity of arr2
print(id(arr2))
  
# making a change in arr1
arr1[1] = 7                       
  
# displaying the arrays
print(arr1)
print(arr2)

输出 :

121258976
125714048
[2 7 9 4]
[2 6 9 4]

这次在一个数组中所做的更改不会反映在另一个数组中。

深拷贝(续)

如果你正在处理 NumPy 矩阵,那么 numpy.copy() 会给你一个深拷贝。但是,如果您的矩阵只是列表的列表,那么在将图像(表示为列表的列表)旋转 90 度的任务中考虑以下两种方法:

Python3

import copy
 
 
def rotate_matrix(image):
    # Copy method one
    copy_image_one = copy.deepcopy(image)
    print("Original", matrix)
    print("Copy of original", copy_image_one)
    N = len(matrix)
 
    # Part 1, reverse order within each row
    for row in range(N):
        for column in range(N):
            copy_image_one[row][column] = image[row][N-column-1]
 
    print("After modification")
    print("Original", matrix)
    print("Copy", copy_image_one)
 
    # Copy method two
    copy_image_two = [list(row) for row in copy_image_one]
    # Test on what happens when you remove list from the above code.
 
    # Part 2, transpose
    for row in range(N):
        for column in range(N):
            copy_image_two[column][row] = copy_image_one[row][column]
 
    return copy_image_two
 
 
if __name__ == "__main__":
    matrix = [[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]]
    print("Rotated image", rotate_matrix(matrix))

输出:

Original [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copy of original [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
After modification
Original [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copy [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
Rotated image [[3, 6, 9], [2, 5, 8], [1, 4, 7]]