深浅copy、第三方模块的下载与安装

一、深浅拷贝详解(重要)

1.对于不可变对象,深拷贝和浅拷贝的效果是一样的,因为不可变对象不需要在内存中复制

2.对于可变对象,深拷贝和浅拷贝的效果是有区别的,主要原因在于可变对象自身的可变性质

1.浅拷贝

1.1 使用数据类型本身的构造器

    list1 = [1, 2, 3]
    list2 = list(list1)
    print(list2)
    print("list1==list2 ?",list1==list2)
    print("list1 is list2 ?",list1 is list2)
    
    set1= set([1, 2, 3])
    set2 = set(set1)
    print(set2)
    print("set1==set2 ?",set1==set2)
    print("set1 is set2 ?",set1 is set2)
    
    dict1 = {1:[1,'w'], 2:0, 3:98}
    dict2 = dict(dict1)
    print(dict2)
    print("dict1 == dict2 ?",dict1 == dict2)
    print("dict1 is dict2 ?",dict1 is dict2)
    
    [1, 2, 3]
    list1==list2 ? True
    list1 is list2 ? False
    
    {1, 2, 3}
    set1==set2 ? True
    set1 is set2 ? False
    
    {1: [1, 'w'], 2: 0, 3: 98}
    dict1 == dict2 ? True
    dict1 is dict2 ? False
    

分析: 浅拷贝,为新变量重新分配一块内存,和原来变量的内存不一样,所以有

    list1 is list2 ? False
    set1 is set2 ? False
    dict1 is dict2 ? False

但浅拷贝完,两个变量中的元素的值是一样的。

    list1==list2 ? True
    dict1 == dict2 ? True
    set1==set2 ? True

1.2 对于列表,还可以通过切片操作符“:”来完成浅拷贝

    list1 = [1, 2, 3]
    list2 = list1[:]
    print(list2)
    print("list1 == list2 ?",list1 == list2)
    print("list1 is list2 ?",list1 is list2)
    
    [1, 2, 3]
    list1 == list2 ? True
    list1 is list2 ? False

1.3 使用 copy.copy() 函数

    函数 copy.copy() 函数,适用于任何数据类型
    import copy
    
    list1 = [1, 2, 3]
    list2 = copy.copy(list1)
    print(list2)
    print("list1 == list2 ?",list1 == list2)
    print("list1 is list2 ?",list1 is list2)
    
    set1 = {1, 2, 3}
    set2 = copy.copy(set1)
    print(set2)
    print("set1 == set2 ?",set1 == set2)
    print("set1 is set2 ?",set1 is set2)
    
    dict1 = {1:'xiaoming', 2:'xiahua',3:'xiaoli'}
    dict2 = dict(dict1)
    print(dict2)
    print("dict1 == dict2 ?",dict1 == dict2)
    print("dict1 is dict2 ?",dict1 is dict2)
    
    [1, 2, 3]
    list1 == list2 ? True
    list1 is list2 ? False
    
    {1, 2, 3}
    set1 == set2 ? True
    set1 is set2 ? False
    
    {1: 'xiaoming', 2: 'xiahua', 3: 'xiaoli'}
    dict1 == dict2 ? True
    dict1 is dict2 ? False

1.4 对于元组,使用 tuple() 或者切片操作符 ‘:’ 不会创建一份浅拷贝,相反它会返回一个指向相同元组的引用

    tuple1 = (1, 2, 3)
    tuple2 = tuple(tuple1)
    print(tuple2)
    print("tuple1 == tuple2 ?",tuple1 == tuple2)
    print("tuple1 is tuple2 ?",tuple1 is tuple2)
    
    tuple1 = (1, 2, 3)
    tuple2 = tuple1[:]
    print(tuple2)
    print("tuple1 == tuple2 ?",tuple1 == tuple2)
    print("tuple1 is tuple2 ?",tuple1 is tuple2)
    
    (1, 2, 3)
    tuple1 == tuple2 ? True
    tuple1 is tuple2 ? True
    
    (1, 2, 3)
    tuple1 == tuple2 ? True
    tuple1 is tuple2 ? True
    

使用 tuple() 或者切片操作符 ‘:’ 不会创建一份浅拷贝,因为它开辟新的内存存储的是原对象的引用,
而没有创建新的对象来存储原对象的子对象的引用,所以不是浅拷贝。相反它会返回一个指向相同元组的引用。

对字符串使用 str() 或者切片操作符 ‘:’,原理和 元组相同

    str1 = 'operation'
    str2 = str1[:]
    print(str2)
    print("str1 == str2 ?",str1 == str2)
    print("str1 is str2 ?",str1 is str2)
    
    operation
    str1 == str2 ? True
    str1 is str2 ? True

    str1 = 'operation'
    str2 = str(str1)
    print(str2)
    print("str1 == str2 ?",str1 == str2)
    print("str1 is str2 ?",str1 is str2)
    
    operation
    str1 == str2 ? True
    str1 is str2 ? True
    

也就是说,对字符串和元组使用 copy()、[:]、本身的构造器完成的复制,
都只是开辟了内存存储原对象的引用,而不是存储原对象的子对象的引用。

2.深拷贝

2.1浅拷贝示例:

    import copy
    
    a = [1, 2, 3]
    b = copy.copy(a)
    b.append(4)
    
    print(f"原始列表a:{a}") # [1, 2, 3]
    print(f"拷贝出来的列表b:{b}") # [1, 2, 3, 4]
    
    上述示例中,我们使用 copy 模块的 copy() 函数实现浅拷贝。
    与深拷贝示例类似,我们向拷贝出的列表 b 中追加了一个值为 4 的元素,
    这没有影响到原始列表 a 的取值0。
    但是需要注意的是,虽然 a 和 b 的地址不同,'但是它们共享部分内存。
    此时,如果我们对共享的数据对象进行了修改,就会出现意外的结果。'

下面是一个浅拷贝中的意外结果的示例:

    import copy
    
    a = [1, 2, [3, 4]]
    b = copy.copy(a)
    b[2].append(5)
    
    print(f"原始列表a:{a}") # [1, 2, [3, 4, 5]]
    print(f"拷贝出来的列表b:{b}") # [1, 2, [3, 4, 5]]

2.2 深拷贝示例

    import copy
    
    a = [1, [2, 3]]
    b = copy.deepcopy(a)
    c = copy.copy(a)
    
    a[1].append(4)
    
    
    print(f"原始列表a:{a}") # [1, [2, 3, 4]]
    print(f"深拷贝出来的列表b:{b}") # [1, [2, 3]]
    print(f"浅拷贝出来的列表c:{c}") # [1, [2, 3, 4]]
    
    在这个示例中,我们拷贝了一个嵌套列表 a,并向其嵌套列表中追加了一个元素。
    随后,我们输出了原始的列表、深拷贝出的 b、以及浅拷贝出的 c。
    输出结果表明,原始列表 a 中嵌套的列表元素变为了 [2, 3, 4]。
    但是,深拷贝出的列表 b 并未受到影响,而浅拷贝出的列表 c 发生了改变。
    这是因为浅拷贝只是复制了一层对象的引用,但是对于嵌套列表中的可变对象,
    它们均指向原始列表 a 中相同的内存地址。因此,当我们修改其中一个列表时,
    其他两者也会受到影响。

总结

         从示例中我们可以看出,深拷贝是对整个对象进行拷贝,而浅拷贝是只复制浅层对象,
    并且共享共同的引用。因此,在实际的编程中,我们需要掌握两者的差异,
    并灵活地选择使用深拷贝还是浅拷贝,

二、第三方模块的下载与安装(重要)


"""内置的模块不能满足我们的需求,所以,大多数的时候都需要借助于第三方模块!"""

第三方模块的下载需要基于网络下载!

如何下载与使用
"""下载第三方模块需要使用pip工具"""
命令行
	不跟版本号,默认装的是最新版本
	pip install 模块名 
    pip install django
    pip install 模块名==版本号
    pip install django == 1.1
    pip install openpyxl==2.1.4
    
 列出解释器中所有的模块名  
 pip list 

"""如何查看模块都有哪些版本号:https://pypi.org/"""

方式2:
	pycharm安装
    
    
 由于官方的服务器在国外,所以,下载模块的时候有可能会很慢或者下载失败
"""
解决办法:
	1. 多试几次下载尝试是否成功
	2. 可能会遇到超时的问题
	3. 换源
"""
 把下载的路径切换到国内
"""
豆瓣:http://pypi.douban.com/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
华为云:https://repo.huaweicloud.com/repository/pypi/simple
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
中科大:https://pypi.mirrors.ustc.edu.cn/simple/
"""

# 如何换源
方式1:
	临时换源
	pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
    """永久换源"""
    搜索windows下如何永久换源
方式2:
	pycharm中换源
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值