找回密码
 会员注册
查看: 33|回复: 0

PythonPython语言基础(中)

[复制链接]

3

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2024-9-5 13:17:19 | 显示全部楼层 |阅读模式
❤️博客主页:iknow181🔥系列专栏:Python、JavaSE、JavaWeb、CCNP🎉欢迎大家点赞👍收藏⭐评论✍目录第十章Python的数据类型基本数据类型数字整数浮点数浮点数的精度问题布尔型进制转换十进制转其它进制其他进制转十进制其它进制转其它进制数字的随机random()方法randint()方法randrange()方法choice()方法sample()方法字符串字符串的定义字符串的方法(1).将字符串的首字母大写(2).字符串全部字母大写(3).将字符串全部内字母全部改成小写(4).去除字符串的首部空白(5).去除字符串的尾部空白(6).去除字符串的所有空白字符串的拼接字符串与整数的切换字符串转整数整数转字符串组合数据类型列表列表的定义访问列表中的元素值确定列表的长度列表的常见操作列表的增加列表的删除列表的修改列表的查看列表的排序1.永久性排序(谨慎使用)2.临时性排序3.反转列表列表解析式列表和字符串之间的转换列表转字符串字符串转列表列表的切片字符串的切片可变数据类型与不可变数据类型元组元组的定义元组元素值的修改修改嵌套在元组里面的列表元素值字典字典的定义字典规范化写法字典的操作访问字典中的值向字典中添加键值对删除键值对修改键值对遍历字典字典的方法操作1.清空所有键值对2.根据key获取value3.更新现有字典中key的value4.以列表形式返回可遍历的(键,值)元组5.将一个字典所有的键生成列表并返回6.以列表返回字典中的所有值7.获取指定key的value,并删除这个key的value8.键值对一起弹出,弹出后变为元组字典的演练第十章Python的数据类型基本数据类型数字整数整数就是整数浮点数在编程中,小数都称之为浮点数浮点数的精度问题print(0.1+0.2)---------------0.30000000000000004​​1.可以通过round()函数来控制小数点后位数round(a+b),则表示结果不保留小数位部分print(round(0.1+0.2))---------------0​round(a+b,x),表示将a+b的结果保留x位小数位。print(round(0.1+0.2,1))----------------0.3​2.格式化输出占位符%d用于整数的占位符%f用于浮点数的占位符%s用于表示字符串的占位符a=5201314b=1314520print("%d+%d的结果为%d"%(a,b,a+b))----------------------5201314+1314520的结果为6515834%.2d:表示一共占2位,不足位补0,超过两位就原样输出%03d:表示一共占3位,不足位补0,超过三位原样输出。%13d:表示一共占13位,不足为补空格,超过十三位原样输出。​a=0.1b=0.2print("%.1f+%.1f的结果为%.1f"%(a,b,a+b))#%.1f表示只保留1位小数部分-------------------------0.1+0.2的结果为0.3布尔型0[False,None]1[True]:所有的非0字符进制转换十进制转其它进制(1).十进制转二进制bin()函数用法:bin(十进制数值)>>>bin(255)'0b11111111'>>>bin(32)'0b100000'​(2).十进制转八进制oct()函数用法:oct(十进制数值)>>>oct(255)'0o377'>>>oct(25)'0o31''运行运行​(3).十进制转十六进制hex()函数用法:hex(十进制数值)>>>hex(150)'0x96'>>>hex(250)'0xfa'其他进制转十进制(1).二进制转十进制int()函数用法:int("二进制数值",2)print(int("1010",2))----------------------10​(2).八进制转十进制int()函数用法:int("八进制数值",8)print(int("15",8))--------------------13​(3).十六进制转十进制int()函数用法:int("十六进制数值",16)print(int("FF",16))--------------------255其它进制转其它进制二进制--->八进制先将二进制的数值转换成十进制,再将十进制的数值转换成八进制  11000011--->八进制a=int("11000011",2)print(a)b=oct(a)print(b)------------------------或print(oct(int("11000011",2)))--------------------0o303数字的随机random模块:可以实现随机的功能importrandomrandom()方法.ranmdom()方法,表示随机生成一个[0,1)的的随机数a=random.random()print(a)------------------------0.06170779578031482randint()方法用于随机生成一个[x,y]之间的一个整数importrandomb=random.randint(0,1)print(b)----------------------0/1randrange()方法用于随机生成一个[x,y]范围内且递增基数为z的整数用法:random.randrange(x,y,z)importrandomb=random.randrange(0,10,2)print(b)-----------------------------------4choice()方法表示从某一个字符串中随机挑选出一个字符用法:random.choice("字符串")importrandomprint(random.choice('Python'))--------------------Psample()方法从某一个字符串中随机挑选出n个字符,并把它们放入列表中用法:random.sample('字符串',n)importrandomprint(random.sample('Python',3))-------------------['y','o','t']请随机生成一个合法的IPv4地址并输出importrandomip_address_1=random.randint(1,239)ip_address_2=random.randint(0,255)ip_address_3=random.randint(0,255)ip_address_4=random.randint(0,255)print("%d.%d.%d.%d"%(ip_address_1,ip_address_2,ip_address_3,ip_address_4))print(ip_address_1,ip_address_2,ip_address_3,ip_address_4,sep='.')----------------182.13.178.37182.13.178.37字符串在Python中,字符串必须用""''引起字符串的定义str1='python'print(str1)print(type(str1))----------------------123字符串的方法(1).将字符串的首字母大写title()方法str1='python'print(str1)print(str1.title())------------------------pythonPython(2).字符串全部字母大写upper()方法str1='python'print(str1)print(str1.upper())------------------------pythonPYTHON(3).将字符串全部内字母全部改成小写lower()str2='NANCY'print(str2)print(str2.lower())----------------------NANCYnancy(4).去除字符串的首部空白lstrip()方法str3=' nihaohahahaha 'print(str3)print(str3.lstrip())-------------------------------- nihaohahahaha nihaohahahaha (5).去除字符串的尾部空白rstrip()str3=' nihaohahahaha 'print(str3)print(str3.rstrip())---------------------------------- nihaohahahaha  nihaohahahaha(6).去除字符串的所有空白.strip()str3=' nihaohahahaha 'print(str3)print(str3.strip())------------------------------------- nihaohahahaha nihaohahahaha字符串的拼接字符串之间使用"+"来进行拼接但必须保证拼接的每一段都是字符串str1='I'str2='am'str3='23'print(str1+""+ str2+""+str3)----------------------------------------Iam23字符串与整数的切换字符串转整数int()函数num1='123'print(num1)print(type(num1))num2=int(num1)print(num2)print(type(num2))--------------------------123123整数转字符串str()函数number1=12345print(number1)print(type(number1))number2=(str(number1))print(number2)print(type(number2))------------------------------1234512345组合数据类型组合数据类型就是指将多个基本数据类型糅合在一块,组成组合数据类型组合数据类型:列表、元组、字典、集合列表列表的定义[]:空列表list1=[]  #空列表list2=[1,2,'Hello']  #定义一个列表print(list2)print(type(list2))--------------------------------[1,2,'Hello']访问列表中的元素值索引列表的索引都是从0开始,依次递增的访问列表中的元素值的方法:list1[元素值所在的索引号]list1=[]  #空列表list2=[1,2,'Hello']  #定义一个列表print(list2[1])------------------------------------2​#如果要快速到取到列表中最后一个索引的元素值,则可以将索引号置为-1list2=[1,2,3,252,253,254,210]print(list2[-1])-------------------------------210​列表的嵌套:list1=[1,2,['A','B','C']]print(list1[2][2])-----------------------C确定列表的长度len():用于确定列表的长度/用于确定列表中有多少个索引list1=[1,2,['A','B','C']]print(len(list1)) -------------------3列表的常见操作#列表的操作:增、删、改、查列表的增加所谓列表的增,就是往列表中添加新的索引及其对应的元素值。(1).append方法:在列表的末尾新增一个元素值​list2=[1,2,3]print(list2)list2.append(4)print(list2)list2.append("A")print(list2)-------------------------[1,2,3][1,2,3,4][1,2,3,4,'A']​(2).insert方法.insert(索引值,元素值)可以在指定索引的位置,添加一个元素的值​list2=[1,2,3]print(list2)list2.insert(2,250)print(list2)--------------------------[1,2,3][1,2,250,3]​扩展:如何使用.insert方法实现.append方法的功能--->(如何使用insert方法总是在列表的末尾插入元素)list2=[1,2,3,252,253,254,210]print(list2)list2.insert(len(list2),'A')print(list2)---------------------[1,2,3,252,253,254,210][1,2,3,252,253,254,210,'A']列表的删除列表的删,也就意味着将某个元素值从列表中删除。(1)del语句dellist[索引值]list2=[1,2,3]dellist2[2]print(list2)dellist2[0]print(list2)-------------------------------[2]​如果要使用del语句快速删除列表中的最后一个元素,则可以使用dellist[-1]的方式list2=[1,2,3,210,2,3,4,5]print(list2[-1]) #用于确认是否可以取到最后一个元素值dellist2[-1]#删除最后一个元素值print(list2)------------------------------5[1,2,3,210,2,3,4]​(2).pop方法list.pop() 如果括号内什么都不加,则默认弹出列表中的最后一个元素list2=[1,2,3,210,2,3,4,5]list2.pop()print(list2)    如果括号内加的是某个元素具体的索引号,则弹出这个索引号上的元素list2=[1,2,3,210,2,3,4,5]list2.pop(0)print(list2)---------------------------------[2,3,210,2,3,4,5]​(3).remove()方法remove(元素值)list2=[1,2,3,210,2,3,4,5]print(list2)list2.remove(210)print(list2)----------------------------------[1,2,3,210,2,3,4,5][1,2,3,2,3,4,5]​#如果列表中存在多个值相同的元素时,则默认只会删除索引靠前的元素list2=[1,2,3,210,2,3,4,5]print(list2)list2.remove(2)print(list2)----------------------------------[1,2,3,210,2,3,4,5][1,3,210,2,3,4,5]列表的修改#所谓列表的修改,就是指将列表中的某个元素值进行修改,不会大动干戈的去动整个列表。#先使用索引号取到原先索引的位置,然后直接进行赋值修改。​list[索引]=元素值list1=[1,2,3]print(list1)list1[2]=6print(list1)---------[1,2,3][1,2,6]列表的查看列表的查看,也叫遍历列表,就是将列表中的元素一个一个的打印出来。​控制结构,循环。理解一下控制结构:所谓的控制结构,就是指不按照原先的顺序执行代码  for变量in集合:  循环体代码​(1).通过访问列表元素来进行遍历---------------------------------list1=['A','B','C',1,2,3]foriinlist1:  print(i)print("查询完成!")--------------------------ABC123查询完成!​(2).通过索引值进行遍历range(n)函数:可以生成[0,n-1]的整数​list1=['A','B','C',1,2,3]foriinlist(range(6)):  print(list1[i])print("大功告成!")---------------------------ABC123大功告成!​(3)通过列表长度来遍历列表list1=['A','B','C',1,2,3]foriinlist(range(len(list1))):  print(list1[i])print("大功告成!")-----------------------ABC123大功告成!​range()函数的补充:range(x,y)#生成从x开始,到y-1为止的整数  range(x,y,z)#生成从x开始,到y-1,且递增基数为z的整数​range()函数的使用示例:生成192.168.1.0/24所有可用的IP地址并输出​list1=[]foriinrange(1,255):  a="192.168.1."+str(i)#字符串拼接  或  #a="192.168.1.%d"%i#格式化输出  list1.append(a)print(list1)----------------------------['192.168.1.1','192.168.1.2','192.168.1.3','192.168.1.4','192.168.1.5','192.168.1.6','192.168.1.7','192.168.1.8','192.168.1.9','192.168.1.10','192.168.1.11','192.168.1.12','192.168.1.13','192.168.1.14','192.168.1.15','192.168.1.16','192.168.1.17','192.168.1.18','192.168.1.19','192.168.1.20','192.168.1.21','192.168.1.22','192.168.1.23','192.168.1.24','192.168.1.25','192.168.1.26','192.168.1.27','192.168.1.28','192.168.1.29','192.168.1.30','192.168.1.31','192.168.1.32','192.168.1.33','192.168.1.34','192.168.1.35','192.168.1.36','192.168.1.37','192.168.1.38','192.168.1.39','192.168.1.40','192.168.1.41','192.168.1.42','192.168.1.43','192.168.1.44','192.168.1.45','192.168.1.46','192.168.1.47','192.168.1.48','192.168.1.49','192.168.1.50','192.168.1.51','192.168.1.52','192.168.1.53','192.168.1.54','192.168.1.55','192.168.1.56','192.168.1.57','192.168.1.58','192.168.1.59','192.168.1.60','192.168.1.61','192.168.1.62','192.168.1.63','192.168.1.64','192.168.1.65','192.168.1.66','192.168.1.67','192.168.1.68','192.168.1.69','192.168.1.70','192.168.1.71','192.168.1.72','192.168.1.73','192.168.1.74','192.168.1.75','192.168.1.76','192.168.1.77','192.168.1.78','192.168.1.79','192.168.1.80','192.168.1.81','192.168.1.82','192.168.1.83','192.168.1.84','192.168.1.85','192.168.1.86','192.168.1.87','192.168.1.88','192.168.1.89','192.168.1.90','192.168.1.91','192.168.1.92','192.168.1.93','192.168.1.94','192.168.1.95','192.168.1.96','192.168.1.97','192.168.1.98','192.168.1.99','192.168.1.100','192.168.1.101','192.168.1.102','192.168.1.103','192.168.1.104','192.168.1.105','192.168.1.106','192.168.1.107','192.168.1.108','192.168.1.109','192.168.1.110','192.168.1.111','192.168.1.112','192.168.1.113','192.168.1.114','192.168.1.115','192.168.1.116','192.168.1.117','192.168.1.118','192.168.1.119','192.168.1.120','192.168.1.121','192.168.1.122','192.168.1.123','192.168.1.124','192.168.1.125','192.168.1.126','192.168.1.127','192.168.1.128','192.168.1.129','192.168.1.130','192.168.1.131','192.168.1.132','192.168.1.133','192.168.1.134','192.168.1.135','192.168.1.136','192.168.1.137','192.168.1.138','192.168.1.139','192.168.1.140','192.168.1.141','192.168.1.142','192.168.1.143','192.168.1.144','192.168.1.145','192.168.1.146','192.168.1.147','192.168.1.148','192.168.1.149','192.168.1.150','192.168.1.151','192.168.1.152','192.168.1.153','192.168.1.154','192.168.1.155','192.168.1.156','192.168.1.157','192.168.1.158','192.168.1.159','192.168.1.160','192.168.1.161','192.168.1.162','192.168.1.163','192.168.1.164','192.168.1.165','192.168.1.166','192.168.1.167','192.168.1.168','192.168.1.169','192.168.1.170','192.168.1.171','192.168.1.172','192.168.1.173','192.168.1.174','192.168.1.175','192.168.1.176','192.168.1.177','192.168.1.178','192.168.1.179','192.168.1.180','192.168.1.181','192.168.1.182','192.168.1.183','192.168.1.184','192.168.1.185','192.168.1.186','192.168.1.187','192.168.1.188','192.168.1.189','192.168.1.190','192.168.1.191','192.168.1.192','192.168.1.193','192.168.1.194','192.168.1.195','192.168.1.196','192.168.1.197','192.168.1.198','192.168.1.199','192.168.1.200','192.168.1.201','192.168.1.202','192.168.1.203','192.168.1.204','192.168.1.205','192.168.1.206','192.168.1.207','192.168.1.208','192.168.1.209','192.168.1.210','192.168.1.211','192.168.1.212','192.168.1.213','192.168.1.214','192.168.1.215','192.168.1.216','192.168.1.217','192.168.1.218','192.168.1.219','192.168.1.220','192.168.1.221','192.168.1.222','192.168.1.223','192.168.1.224','192.168.1.225','192.168.1.226','192.168.1.227','192.168.1.228','192.168.1.229','192.168.1.230','192.168.1.231','192.168.1.232','192.168.1.233','192.168.1.234','192.168.1.235','192.168.1.236','192.168.1.237','192.168.1.238','192.168.1.239','192.168.1.240','192.168.1.241','192.168.1.242','192.168.1.243','192.168.1.244','192.168.1.245','192.168.1.246','192.168.1.247','192.168.1.248','192.168.1.249','192.168.1.250','192.168.1.251','192.168.1.252','192.168.1.253','192.168.1.254']列表的排序列表的排序是指将列表中所有元素的值进行比较,按照一定的顺序来进行排列。升序:从小到大降序:从大到小1.永久性排序(谨慎使用).sort()方法升序:list1=[1,10,3,5,2,6]print(list1)list1.sort()print(list1)---------------------------[1,10,3,5,2,6][1,2,3,5,6,10]​降序:list1=[1,10,3,5,2,6]print(list1)list1.sort(reverse=True)print(list1)[1,10,3,5,2,6][10,6,5,3,2,1]永久性排序结束后,列表就再也回不到之前的形态了。如果要永久性排序,建议先备份原列表。2.临时性排序sorted()函数升序:​list1=[1,10,3,5,2,6]print(list1)print(sorted(list1))print(list1)-----------------------------[1,10,3,5,2,6][1,2,3,5,6,10][1,10,3,5,2,6]​降序:list1=[1,10,3,5,2,6]print(list1)print(sorted(list1,reverse=True))print(list1)---------------------[1,10,3,5,2,6][10,6,5,3,2,1][1,10,3,5,2,6]3.反转列表.reverse()方法永久性反转list1=[1,10,3,5,2,6]print(list1)list1.reverse()print(list1)-----------------------[1,10,3,5,2,6][6,2,5,3,10,1]列表解析式列表解析式一个固定格式的东西列表中存放的是一个变量的值,这个变量值在后面的循环中依次赋值list1=[iforiinrange(2,101,2)]#后面的for循环为i提供值print(list1)----------------------------------[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100]列表和字符串之间的转换列表转字符串列表转字符串:str()函数list1=['CCIE']print(list1)print(type(list1))str1=str(list1)print(str1)print(type(str1))-----------------------['CCIE']['CCIE']​​list1=['C','C','I','E']str1=str(list1)print(str1)str2=list1[0]+list1[1]+list1[2]+list1[3]print(str2)--------------------------['C','C','I','E']CCIE字符串转列表list()函数str1='Putty'print(str1)print(type(str1))list1=list(str1)print(list1)print(type(list1))----------------------------Putty['P','u','t','t','y']'运行运行列表的切片当列表范围过大,需要只取部分值的时候list[起始索引:结束索引+1]​list1=[iforiinrange(2,101,2)]print(list1)list2=list1[0:5]print(list2)list3=list1[5:10]print(list3)-----------------------------------[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100][2,4,6,8,10][12,14,16,18,20]字符串的切片方法一:先将字符串转为列表,进行列表的切片,切完之后再转回字符串str1='HappyNationalDay'list1=list(str1)print(list(str1))print(list1)list2=list1[0:5]print(list2)str2=list1[0]+list1[1]+list1[2]+list1[3]+list1[4]print(str2)print(type(str2))-----------------------------['H','a','p','p','y','N','a','t','i','o','n','a','l','D','a','y']['H','a','p','p','y','N','a','t','i','o','n','a','l','D','a','y']['H','a','p','p','y']Happy​​字符串的方法:.join()方法  将列表内的字符一一拼接  str1='HappyNationalDay'list1=list(str1) #将字符串转为列表list2=list1[0:5] #进行列表的切片[起始索引:结束索引+1]str2="" #定义一个新的空字符串str2=str2.join(list2) #使用join()方法将list2中的所有元素作为字符串,填入str2中print(str2)----------------------------------Happy​方法二:直接对字符串进行切片,方法和列表类似,[起始索引:结束索引+1]str1='HappyNationalDay'str2=str1[0:5]print(str2)-----------------------------------Happy​方法三:split()方法,可以在指定的字符处切片str1='192.168.11.128'str2=str1.split('.')print(str2)-----------------------------['192','168','11','128']​​str1='192.168.11.128'str2=str1.split('.',maxsplit=1) #maxsplit用于控制切的次数print(str2)-------------------------------------['192','168.11.128']可变数据类型与不可变数据类型可变数据类型:列表,字典不可变数据类型:元组。描述性地址相同的是可变数据类型,描述性地址不相同的是不可变数据类型。#id()函数用于查看存放在内存中的地址a=[1,2,3]print(id(a))  #查看a这个列表存储在内存中的地址-----------------2439173331328  #描述性地址元组元组的定义元组的定义:()tuple1=()  #空元组tuple2=(1,2,3)print(tuple2)print(type(tuple2))(1,2,3)元组元素值的修改元组不能直接修改元素值,但是列表可以所以,可以先把元组转成列表修改,修改完再转回元组。列表转元组:tuple(list)  元组转列表:list(tuple)第一步,将元组转为列表tuple2=(1,2,3)list2=list(tuple2)print(list2)print(type(list2))​第二步,在列表中进行元素值的修改list2[2]=300print(list2)​第三步,将修改好元素的列表转回为元组,输出元组tuple2=tuple(list2)print(tuple2)print(type(tuple2))---------------------------------------[1,2,3][1,2,300](1,2,300)修改嵌套在元组里面的列表元素值tuple8=(1,2,[1,2,3])print(tuple8)tuple8[2][2]=300print(tuple8)----------------------(1,2,[1,2,3])(1,2,[1,2,300])字典字典是组合数据类型中的可变数据类型字典的组成:键值对一个键对应一个值  一个键可以有多个值。字典的定义{}dict1={} #空字典dict2={"R1":"192.168.1.1","Mask":"/24"}print(dict2)-----------------------------------{'R1':'192.168.1.1','Mask':'/24'}字典规范化写法dict1={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.123.1.1",  "Lo0Mask":"255.255.255.0"}字典的操作访问字典中的值通过键来访问值dict2={"R1":"192.168.1.1","Mask":"/24"}print(dict2["Mask"])------------------------------------/24向字典中添加键值对dict2={"R1":"192.168.1.1","Mask":"/24"}dict2["Description"]="ThisisR1" #dict2["键"]="值"print(dict2)-----------------------------{'R1':'192.168.1.1','Mask':'/24','Description':'ThisisR1'}删除键值对del语句dict1={} #空字典dict2={"R1":"192.168.1.1","Mask":"/24"}deldict2["R1"]print(dict2)-------------------------{'Mask':'/24'}修改键值对通过访问键,重新赋值。dict2={"R1":"192.168.1.1","Mask":"/24"}dict2["R1"]="10.1.1.1"print(dict2)--------------------------{'R1':'10.1.1.1','Mask':'/24'}遍历字典(1)遍历所有的键值对.items():可以遍历字典中的所有键值对forkey,valueindict1.items():  print("这是键:"+key)  print("这是键:"+value)----------------------------------这是键:device这是键:CiscoIOS-L3Switch这是键escription这是键:ThisisaCore-Switch这是键o0IPAddress这是键:10.123.1.1这是键o0Mask这是键:255.255.255.0​​(2).遍历所有的键forkeyindict1.keys():  print(key)---------------------------deviceDescriptionLo0IPAddressLo0Mask​(3).遍历所有的值forvalueindict1.values():  print(value)-----------------------------------CiscoIOS-L3SwitchThisisaCore-Switch10.123.1.1255.255.255.0字典的方法操作1.清空所有键值对.clear()方法:dict1={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.123.1.1",  "Lo0Mask":"255.255.255.0"}print(dict1)dict1.clear()print(dict1)------------------------{'device':'CiscoIOS-L3Switch','Description':'ThisisaCore-Switch','Lo0IPAddress':'10.123.1.1','Lo0Mask':'255.255.255.0'}{}2.根据key获取value.get()方法,相当于是[]的增强版。如果get后面跟的键,在字典中存在且有对应值,则直接返回指。如果get后面跟的键,在字典不存在,则返回None而非报错dict1={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.123.1.1",  "Lo0Mask":"255.255.255.0"}device_id=dict1.get("device")print(device_id)------------------------CiscoIOS-L3Switch3.更新现有字典中key的value.update()方法使用新字典中的值替换旧字典中的值dict1={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.123.1.1",  "Lo0Mask":"255.255.255.0"}dict2={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.1.1.1",  "Lo0Mask":"255.255.255.0"}dict1.update(dict2)  #用dict2来更新dict1print(dict1)-------------------------------{'device':'CiscoIOS-L3Switch','Description':'ThisisaCore-Switch','Lo0IPAddress':'10.1.1.1','Lo0Mask':'255.255.255.0'}4.以列表形式返回可遍历的(键,值)元组.items()方法dict2={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.1.1.1",  "Lo0Mask":"255.255.255.0"}key_value=dict2.items()print(key_value)----------------------------------dict_items([('device','CiscoIOS-L3Switch'),('Description','ThisisaCore-Switch'),('Lo0IPAddress','10.1.1.1'),('Lo0Mask','255.255.255.0')])5.将一个字典所有的键生成列表并返回.keys()方法​dict2={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.1.1.1",  "Lo0Mask":"255.255.255.0"}key=dict2.keys()print(key)-----------------------------------------dict_keys(['device','Description','Lo0IPAddress','Lo0Mask'])​6.以列表返回字典中的所有值.values()方法dict2={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.1.1.1",  "Lo0Mask":"255.255.255.0"}values=dict2.values()print(values)---------------------------------dict_values(['CiscoIOS-L3Switch','ThisisaCore-Switch','10.1.1.1','255.255.255.0'])7.获取指定key的value,并删除这个key的value.pop()方法#弹出根据key弹出valuedict2={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.1.1.1",  "Lo0Mask":"255.255.255.0"}pop_key=dict2.pop("Description")print(dict2)print(pop_key)--------------------{'device':'CiscoIOS-L3Switch','Lo0IPAddress':'10.1.1.1','Lo0Mask':'255.255.255.0'}ThisisaCore-Switch8.键值对一起弹出,弹出后变为元组.popitems()dict2={  "device":"CiscoIOS-L3Switch",  "Description":"ThisisaCore-Switch",  "Lo0IPAddress":"10.1.1.1",  "Lo0Mask":"255.255.255.0"}print(dict2)dict2.popitem()print(dict2)--------------------{'device':'CiscoIOS-L3Switch','Description':'ThisisaCore-Switch','Lo0IPAddress':'10.1.1.1','Lo0Mask':'255.255.255.0'}{'device':'CiscoIOS-L3Switch','Description':'ThisisaCore-Switch','Lo0IPAddress':'10.1.1.1'}字典的演练请按照以下要求生成一个字典:字典中的键为R1、R2、R3、R4等共计50个(R1....R50)  每个键对应的值为一个随机生成的IP地址。​importrandomdict1={}foriinrange(1,51):  ip_add_1=random.randint(0,239)  ip_add_2=random.randint(0,256)  ip_add_3=random.randint(0,256)  ip_add_4=random.randint(0,256)  ip_add="%d.%d.%d.%d"%(ip_add_1,ip_add_2,ip_add_3,ip_add_4)  key="R"+str(i) 或 key="R%s"%i  dict1[key]=ip_addprint(dict1)---------------------{'R1':'118.35.62.190','R2':'168.3.24.235','R3':'116.248.67.84','R4':'63.98.30.57','R5':'184.117.11.151','R6':'231.13.14.229','R7':'204.146.102.2','R8':'30.67.193.142','R9':'103.147.193.241','R10':'152.35.111.245','R11':'102.116.239.167','R12':'121.175.89.60','R13':'152.212.41.252','R14':'120.12.47.64','R15':'103.19.58.104','R16':'175.227.69.134','R17':'44.250.4.191','R18':'61.109.194.212','R19':'185.6.183.147','R20':'38.239.59.52','R21':'31.247.205.187','R22':'97.53.203.194','R23':'94.175.165.69','R24':'209.45.42.86','R25':'13.190.147.99','R26':'93.124.123.116','R27':'239.126.116.77','R28':'97.160.18.72','R29':'56.67.163.47','R30':'45.199.174.11','R31':'178.175.249.103','R32':'122.164.248.197','R33':'147.133.82.37','R34':'1.69.201.174','R35':'100.244.111.91','R36':'78.238.42.109','R37':'91.4.236.82','R38':'215.220.221.123','R39':'120.78.63.190','R40':'77.20.18.128','R41':'86.242.90.88','R42':'178.199.167.228','R43':'106.86.256.176','R44':'164.100.39.62','R45':'171.35.153.11','R46':'138.85.61.59','R47':'131.13.91.225','R48':'44.60.75.88','R49':'59.135.155.208','R50':'125.55.13.223'}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-7 06:32 , Processed in 1.088378 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表