本文主要是介绍NumPy(十三):数组的复制【.copy()】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import numpy as np# 数组的复制ar1 = np.arange(10)
ar2 = ar1
print('ar1 = {0}, ar2 = {1}'.format(ar1, ar2))
print('ar2 is ar1: ', ar2 is ar1)# 回忆python的赋值逻辑:指向内存中生成的一个值 → 这里ar1和ar2指向同一个值,所以ar1改变,ar2一起改变
ar1[2] = 9
print('ar1 = {0}, ar2 = {1}'.format(ar1, ar2))print('-' * 100)# copy方法生成数组及其数据的完整拷贝
ar3 = ar1.copy()
print('ar1 = {0}, ar3 = {1}'.format(ar1, ar3))
print('ar3 is ar1: ', ar3 is ar1)
ar1[0] = 9
print('ar1 = {0}, ar3 = {1}'.format(ar1, ar3))
打印结果:
ar1 = [0 1 2 3 4 5 6 7 8 9], ar2 = [0 1 2 3 4 5 6 7 8 9]
ar2 is ar1: True
ar1 = [0 1 9 3 4 5 6 7 8 9], ar2 = [0 1 9 3 4 5 6 7 8 9]
----------------------------------------------------------------------------------------------------
ar1 = [0 1 9 3 4 5 6 7 8 9], ar3 = [0 1 9 3 4 5 6 7 8 9]
ar3 is ar1: False
ar1 = [9 1 9 3 4 5 6 7 8 9], ar3 = [0 1 9 3 4 5 6 7 8 9]Process finished with exit code 0
这篇关于NumPy(十三):数组的复制【.copy()】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!