zoukankan      html  css  js  c++  java
  • Numpy的学习6-深浅赋值(copy&deep copy)

    # = 的赋值方式会带有关联性
    
    
    import numpy as np
    
    a = np.arange(4)
    # array([0, 1, 2, 3])
    
    b = a
    c = a
    d = b
    
    # 改变a的第一个值,b、c、d的第一个值也会同时改变。
    
    a[0] = 11
    print(a)
    # array([11,  1,  2,  3])
    
    # 确认b、c、d是否与a相同。
    
    b is a  # True
    c is a  # True
    d is a  # True
    
    # 同样更改d的值,a、b、c也会改变。
    
    d[1:3] = [22, 33]   # array([11, 22, 33,  3])
    print(a)            # array([11, 22, 33,  3])
    print(b)            # array([11, 22, 33,  3])
    print(c)            # array([11, 22, 33,  3])
    
    # copy() 的赋值方式没有关联性
    
    b = a.copy()    # deep copy
    print(b)        # array([11, 22, 33,  3])
    a[3] = 44
    print(a)        # array([11, 22, 33, 44])
    print(b)        # array([11, 22, 33,  3])
    
    # 此时a与b已经没有关联。
    

     = 的赋值方式会带有关联性(a=b,当b改变a随之改变),copy没有

  • 相关阅读:
    window10-jmeter安装
    软件开发模型
    软件测试的原则和流程
    手机APP测试
    优秀的软件测试工程师
    自动化测试
    测试用例
    软件测试功能分类
    ios-prefix文件最全
    催收策略及催收评分卡搭建
  • 原文地址:https://www.cnblogs.com/simon-idea/p/9571308.html
Copyright © 2011-2022 走看看