首页 > 编程语言 >python基础25

python基础25

时间:2023-06-14 16:01:15浏览次数:43  
标签:25 python list1 基础 list2 tuple2 print 拷贝 copy

第三方模块的下载与安装

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

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

    如何下载和使用

      下载第三方模块需要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]]

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

  总结

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

  

 

 

 

          

          

        

 

 

 

 

 

 

      

标签:25,python,list1,基础,list2,tuple2,print,拷贝,copy
From: https://www.cnblogs.com/shanghaipudong/p/17480506.html

相关文章

  • python匿名函数学习笔记
    当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便。list(map(lambdax:x*x,[1,2,3,4,5,6,7,8,9]))[1,4,9,16,25,36,49,64,81]由此,匿名函数lambdax:x*x实际上就是:deff(x):returnx*x关键字lambda表示匿名函数,冒号前......
  • MySQL基础:安装和启动
    MySQL基础:安装和启动课程安排基础篇介绍数据库相关概念SQL是操作关系型数据库的编程语言主流关系型数据库总结安装MySQL数据库MySQL数据库启动与停止默认mysql开机启动客户端连接方式1方式2需要配置环境变量......
  • python 操作文件 筛选 glob
    importglobimportosstr_addr=r"D:\360极速浏览器下载"str_join=os.path.join(str_addr,"*.*")glob.glob(str_join)list(glob.glob(str_join))==glob.glob(str_join)list(glob.iglob(str_join))==glob.glob(str_join)https://cloud.tencent.com/d......
  • [ARM汇编]计算机原理与数制基础—1.1.3 二进制补码
    在计算机中,为了表示有符号整数(即正数和负数),通常采用二进制补码表示法。二进制补码不仅可以表示负数,还能简化计算机的加法和减法运算。接下来,我们将介绍二进制补码的概念及其计算方法。原码、反码和补码在讨论补码之前,我们先了解一下原码和反码的概念。原码:直接将一个有符号整......
  • 【技术积累】Python中的NumPy库【二】
    NumPy库的主要类有哪些?NumPy库的主要类包括:ndarray:N维数组对象,是NumPy最重要的类之一。它是Python中数组的基本数据结构,可以进行高效的数学计算和数据处理操作。ufunc:通用函数对象,是NumPy库中的另一个重要类。它是一种高效的元素级运算工具,提供了基本......
  • On Python
    Chapter11Test-DrivenDevelopmentTest-DrivenDevelopmentPrinciplesTDDconsistsofwritingtestcasesthatcoveradesiredfeature,thenwritingthefeatureitself.Inotherwords,theusuageexamplesarewrittenbeforethecodeevenexists.......
  • 对python生成器的理解
    什么是生成器?yield该函数没有运行而是返回了一个对象生成器是迭代器需要满足迭代器协议yield对函数做了什么和class定义的迭代器进行对比创建生成器要创建一个generator,有很多种方法。第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个generator:L......
  • Vue.js 组件基础 #yyds干货盘点#【yyds干货盘点】
    学习目录:Vue.js简介Vue.js实例与数据绑定Vue.js计算属性和侦听器Vue.js条件渲染和列表渲染Vue.js事件处理Vue.js表单输入绑定Vue.js组件基础Vue.js组件通信Vue.js插槽Vue.js动态组件和异步组件Vue.js自定义指令Vue.js过渡和动画Vue.js混入Vue.js自定义事件和v-model......
  • python 操作文件/文件夹 案例
    importosimportshutilimportglobstr_input=input("输入文件夹名即格式:")str_addr=r"D:\360极速浏览器下载"str_dest=os.path.join(str_addr,str_input)list_glob=list(glob.glob(os.path.join(str_addr,"*."+str_input+"*")))&......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-3-离线搭建playwright环境
    1.简介有些小伙伴或者童鞋们私信留言说自己是在公司局域网办公,或者公司为了安全对网络管控比较严格(尤其是一些大的国企、央企),总之就是一句话无法连到外网去在线下载,宏哥刚看到留言时觉得这问题还留言问啊,你找个有网的电脑下载好安装包然后安装就可以用了。(第一种情况及解决办法:带......