zoukankan      html  css  js  c++  java
  • 赋值、浅拷贝以及深拷贝的区别

    字符串赋值

    >>>
    >>> str1 = 'standby'
    >>> 
    >>> str2 = str1
    >>> 
    >>> print str1==str2, str1 is str2
    True True
    >>> 
    >>> id(str1)
    139792206350496
    >>> id(str2)
    139792206350496
    >>> 

    补充

    >>> 
    >>> a = 'standby'
    >>> b = 'standby'
    >>> 
    >>> a == b
    True
    >>> a is b
    True
    >>> 
    >>> id(a)
    139792206350496
    >>> id(b)
    139792206350496
    >>> 

    字符串浅拷贝方式一

    >>> 
    >>> import copy
    >>> str1 = 'standby'
    >>> 
    >>> str2 = copy.copy(str1)
    >>> 
    >>> print str1==str2, str1 is str2
    True True
    >>> 
    >>> id(str1)
    139792206350496
    >>> id(str2)
    139792206350496
    >>> 

    字符串浅拷贝方式二

    >>> 
    >>> str1 = 'standby'
    >>> 
    >>> str2 = str1[:]
    >>> 
    >>> print str1==str2, str1 is str2
    True True
    >>> 
    >>> id(str1)
    139792206350496
    >>> id(str2)
    139792206350496
    >>> 
    

    字符串深拷贝

    >>> 
    >>> import copy
    >>> str1 = 'standby'
    >>> 
    >>> str2 = copy.deepcopy(str1)
    >>> 
    >>> print str1==str2, str1 is str2
    True True
    >>> 
    >>> id(str1)
    139792206350496
    >>> id(str2)
    139792206350496
    >>> 
    

    列表赋值

    >>> 
    >>> list1 = [1, 2, [1, 2]]
    >>> 
    >>> list2 = list1
    >>> 
    >>> print list1==list2, list1 is list2
    True True
    >>> 
    >>> id(list1)
    139792206344992
    >>> id(list2)
    139792206344992
    >>> 

    补充

    >>> 
    >>> list1 = [1, 2, ['a', 'c']]
    >>> list2 = [1, 2, ['a', 'c']]
    >>> 
    >>> list1 == list2
    True
    >>> list1 is list2
    False
    >>> 
    >>> id(list1)
    139792206356704
    >>> id(list2)
    139792206348088
    >>> 

    列表浅拷贝方式一

    >>> 
    >>> import copy
    >>> list1 = [1, 2, [1, 2]]
    >>> 
    >>> list2 = copy.copy(list1)
    >>> 
    >>> print list1==list2, list1 is list2
    True False
    >>> 
    >>> 
    >>> id(list1)
    139792206344920
    >>> id(list2)
    139792206356560
    >>> 
    

    列表浅拷贝方式二

    >>> 
    >>> list1 = [1, 2, [1, 2]]
    >>> 
    >>> list2 = list1[:]
    >>> 
    >>> print list1==list2, list1 is list2
    True False
    >>> 
    >>> id(list1)
    139792206356704
    >>> id(list2)
    139792206344992
    >>> 
    

      

    列表深拷贝

    >>> 
    >>> import copy
    >>> list1 = [1, 2, 3, 4]
    >>> 
    >>> list2 = copy.deepcopy(list1)
    >>> 
    >>> print list1==list2, list1 is list2
    True False
    >>> 
    >>> id(list1)
    139792206344992
    >>> id(list2)
    139792206344920
    >>> 

    浅拷贝情况下的修改

    >>> 
    >>> import copy
    >>> list1 = [1, 2, ['a', 'c']]
    >>> 
    >>> list2 = copy.copy(list1)
    >>> print list1==list2, list1 is list2
    True False
    >>> id(list1)
    139792206358720
    >>> id(list2)
    139792206359080
    >>> 
    >>> list2[2].append('k')
    >>> print list2,list1
    [1, 2, ['a', 'c', 'k']] [1, 2, ['a', 'c', 'k']]
    >>> id(list1)
    139792206358720
    >>> id(list2)
    139792206359080
    >>> 
    

      

    深拷贝情况下的修改

    >>> 
    >>> import copy
    >>> list1 = [1, 2, ['a', 'c']]
    >>> 
    >>> list2 = copy.deepcopy(list1)
    >>> print list1==list2, list1 is list2
    True False
    >>> id(list1)
    139792206344920
    >>> id(list2)
    139792206358720
    >>> 
    >>> list2[2].append('k')
    >>> print list2,list1
    [1, 2, ['a', 'c', 'k']] [1, 2, ['a', 'c']]
    >>> id(list1)
    139792206344920
    >>> id(list2)
    139792206358720
    >>> 
    

      

     

    作者:Standby一生热爱名山大川、草原沙漠,还有妹子
    出处:http://www.cnblogs.com/standby/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    字符串算法—正则表达式
    字符串算法—字符串搜索
    字符串算法—字典树
    字符串算法—字符串排序(下篇)
    字符串算法—字符串排序(上篇)
    图表算法—最短路径
    基本算法——前缀和与差分
    图论——图的表示
    基本算法——康托展开与逆康托展开
    基本算法——离散化
  • 原文地址:https://www.cnblogs.com/standby/p/8322307.html
Copyright © 2011-2022 走看看