python基础25

第三方模块的下载与安装

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

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

    如何下载和使用

      下载第三方模块需要pip工具

  方式一:

    命令行

pip install 模块名 
pip install django
pip install 模块名==版本号
pip install django == 1.1
pip install openpyxl==2.1.4

  不跟版本号,默认装最新版本

 pip list # 列出解释器中所有的模块名

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

  方式二:

    pycharm安装

      file----project----python interpreter

  由于官方的服务器在国外,所以,下载模块的时候有可能会很慢或者下载失败

    解决办法:

      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/

      4.如何换源

        方式一:

          pip install requests -i(i 是路径)临时换源

        方式二:

          pycharm中换源

深浅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 = [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()函数,适用于任何数据类型

复制代码
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
复制代码

  字符串使用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
复制代码

 字符串和元组都是引用原来的储存对象,而不是向列表、字典开辟出新的内存空间

 深拷贝

    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()函数实现浅拷贝,在拷贝出的对象中添加值,不会改变原来对象中的值,但是他们可以共性部分内存,此时我们修改共享部分内存得值就会两个对象都修改。

    2.共享内存的示例

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]]

    3.深拷贝示例

复制代码
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]]
复制代码

  以上示中,深拷贝并未受到共享内存的影响,而浅拷贝受到影响。这是因为浅拷贝只是拷贝一层对象的引用,但是嵌套在里面的可变对象,内存地址是共享的。深拷贝则是完全拷贝出来,与原来的对象的内存地址没有任何关系。

  总结

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

  

 

 

 

          

          

        

 

 

 

 

 

 

      

posted @   Py玩家  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示