python基础day25 深浅copy和第三方模块下载使用

发布时间 2023-06-14 15:55:57作者: 追逐的小白

深浅copy

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

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

浅拷贝

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

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

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

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

使用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

对于元组,使用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()、“:”、本身的构造器完成的复制,都只是开辟了内存存储对象的引用,而不是存储原对象的子对象的引用。

深拷贝

浅拷贝示例

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 的取值。但是需要注意的是,虽然 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]]

深拷贝示例

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中换源

ATM项目

项目开发流程
    公司的类型:
        1. 外包公司
            
        2. 甲方公司
 几个人员角色:
    开发人员(后端开发)、前端、测试、运营、产品经理、运维、销售、UI(设计)等
  
# 需求一般出自于:运营和销售人员----->产品经理(原型图)----->UI---->前端(用代码去实现)---->后端程序员(结合原型图和设计图还有前端给你代码或者意见)(数据库,字段,后端逻辑之间的关系,最好画一个流程图出来)---->自测----->测试------>上线(运维)

# 代码仓库(git)-----》分支------》几个分支----->每个分支什么意思
dev test release master
...