Python 官方文档:入门教程 => 点击学习
❤️博客主页: iknow181 🔥系列专栏: Python、JavaSE、JavaWeb、CCNP 🎉欢迎大家点赞👍收藏⭐评论✍ 目录 第十章 Python的数据类型 基本数据类型
❤️博客主页: iknow181
🔥系列专栏: Python、JavaSE、JavaWeb、CCNP
🎉欢迎大家点赞👍收藏⭐评论✍
目录
7.获取指定key的value,并删除这个key的value
整数就是整数
在编程中,小数都称之为浮点数
print(0.1 + 0.2)---------------0.30000000000000004
1.可以通过round()函数来控制小数点后位数round(a + b),则表示结果不保留小数位部分print(round(0.1 + 0.2))---------------0round(a + b, x),表示将a+b的结果保留x位小数位。print(round(0.1 + 0.2, 1))----------------0.32.格式化输出占位符%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模块:可以实现随机的功能import random
.ranmdom()方法,表示随机生成一个[0,1)的的随机数a = random.random()print(a)------------------------0.06170779578031482
用于随机生成一个[x,y]之间的一个整数import randomb = random.randint(0, 1)print(b)----------------------0/1
用于随机生成一个[x,y]范围内且递增基数为z的整数用法:random.randrange(x, y, z)import randomb = random.randrange(0, 10, 2)print(b)-----------------------------------4
表示从某一个字符串中随机挑选出一个字符用法: random.choice("字符串")import randomprint(random.choice('Python'))--------------------P
从某一个字符串中随机挑选出n个字符,并把它们放入列表中用法: random.sample('字符串', n)import randomprint(random.sample('Python', 3))-------------------['y', 'o', 't']
请随机生成一个合法的IPv4地址并输出import randomip_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
title()方法
str1 = 'python'print(str1)print(str1.title())------------------------pythonPython
upper()方法
str1 = 'python'print(str1)print(str1.upper())------------------------pythonPYTHON
lower()
str2 = 'NANCY'print(str2)print(str2.lower())----------------------NANCYnancy
lstrip()方法
str3 = ' nihaohahahaha 'print(str3)print(str3.lstrip())-------------------------------- nihaohahahaha nihaohahahaha
rstrip()
str3 = ' nihaohahahaha 'print(str3)print(str3.rstrip())---------------------------------- nihaohahahaha nihaohahahaha
.strip()
str3 = ' nihaohahahaha 'print(str3)print(str3.strip())------------------------------------- nihaohahahaha nihaohahahaha
字符串之间使用"+"来进行拼接但必须保证拼接的每一段都是字符串str1 = 'I'str2 = 'am'str3 = '23'print(str1 + " " + str2 + " " + str3)----------------------------------------I am 23
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语句 del list[索引值]
list2 = [1, 2, 3]del list2[2]print(list2)del list2[0]print(list2)-------------------------------[2]
如果要使用del语句快速删除列表中的最后一个元素,则可以使用del list[-1]的方式
list2 = [1, 2, 3, 210, 2, 3, 4, 5]print(list2[-1]) # 用于确认是否可以取到最后一个元素值del list2[-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]for i in list1: print(i)print("查询完成!")--------------------------ABC123查询完成!
(2).通过索引值进行遍历range(n)函数:可以生成[0,n-1]的整数
list1 = ['A', 'B', 'C', 1, 2, 3]for i in list(range(6)): print(list1[i])print("大功告成!")---------------------------ABC123大功告成!
(3)通过列表长度来遍历列表
list1 = ['A', 'B', 'C', 1, 2, 3]for i in list(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 = []for i in range(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']
列表的排序是指将列表中所有元素的值进行比较,按照一定的顺序来进行排列。升序:从小到大降序:从大到小
.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]
永久性排序结束后,列表就再也回不到之前的形态了。如果要永久性排序,建议先备份原列表。
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]
.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 = [i for i in range(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 = [i for i in range(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": "Cisco iOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.123.1.1", "Lo0 Mask": "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"] = "This is R1" # dict2["键"] = "值"print(dict2)-----------------------------{'R1': '192.168.1.1', 'Mask': '/24', 'Description': 'This is R1'}
del语句
dict1 = {} # 空字典dict2 = {"R1": "192.168.1.1", "Mask": "/24"}del dict2["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():可以遍历字典中的所有键值对
for key, value in dict1.items(): print("这是键:" + key) print("这是键:" + value)----------------------------------这是键:device这是键:Cisco IOS-L3 Switch这是键:Description这是键:This is a Core-Switch这是键:Lo0 IP Address这是键:10.123.1.1这是键:Lo0 Mask这是键:255.255.255.0
(2).遍历所有的键
for key in dict1.keys(): print(key)---------------------------deviceDescriptionLo0 IP AddressLo0 Mask
(3).遍历所有的值
for value in dict1.values(): print(value)-----------------------------------Cisco IOS-L3 SwitchThis is a Core-Switch10.123.1.1255.255.255.0
.clear()方法:
dict1 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.123.1.1", "Lo0 Mask": "255.255.255.0"}print(dict1)dict1.clear()print(dict1)------------------------{'device': 'Cisco IOS-L3 Switch', 'Description': 'This is a Core-Switch', 'Lo0 IP Address': '10.123.1.1', 'Lo0 Mask': '255.255.255.0'}{}
.get()方法,相当于是[]的增强版。 如果get后面跟的键,在字典中存在且有对应值,则直接返回指。 如果get后面跟的键,在字典不存在,则返回None而非报错
dict1 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.123.1.1", "Lo0 Mask": "255.255.255.0"}device_id = dict1.get("device")print(device_id)------------------------Cisco IOS-L3 Switch
.update()方法 使用新字典中的值替换旧字典中的值
dict1 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.123.1.1", "Lo0 Mask": "255.255.255.0"}dict2 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.1.1.1", "Lo0 Mask": "255.255.255.0"}dict1.update(dict2) # 用dict2来更新dict1print(dict1)------------------------------- {'device': 'Cisco IOS-L3 Switch', 'Description': 'This is a Core-Switch', 'Lo0 IP Address': '10.1.1.1', 'Lo0 Mask': '255.255.255.0'}
.items()方法
dict2 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.1.1.1", "Lo0 Mask": "255.255.255.0"}key_value = dict2.items()print(key_value)----------------------------------dict_items([('device', 'Cisco IOS-L3 Switch'), ('Description', 'This is a Core-Switch'), ('Lo0 IP Address', '10.1.1.1'), ('Lo0 Mask', '255.255.255.0')])
.keys()方法
dict2 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.1.1.1", "Lo0 Mask": "255.255.255.0"}key = dict2.keys()print(key)-----------------------------------------dict_keys(['device', 'Description', 'Lo0 IP Address', 'Lo0 Mask'])
.values()方法
dict2 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.1.1.1", "Lo0 Mask": "255.255.255.0"}values = dict2.values()print(values)---------------------------------dict_values(['Cisco IOS-L3 Switch', 'This is a Core-Switch', '10.1.1.1', '255.255.255.0'])
.pop()方法 #弹出 根据key弹出value
dict2 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.1.1.1", "Lo0 Mask": "255.255.255.0"}pop_key = dict2.pop("Description")print(dict2)print(pop_key)--------------------{'device': 'Cisco IOS-L3 Switch', 'Lo0 IP Address': '10.1.1.1', 'Lo0 Mask': '255.255.255.0'}This is a Core-Switch
.popitems()
dict2 = { "device": "Cisco IOS-L3 Switch", "Description": "This is a Core-Switch", "Lo0 IP Address": "10.1.1.1", "Lo0 Mask": "255.255.255.0"}print(dict2)dict2.popitem()print(dict2)--------------------{'device': 'Cisco IOS-L3 Switch', 'Description': 'This is a Core-Switch', 'Lo0 IP Address': '10.1.1.1', 'Lo0 Mask': '255.255.255.0'}{'device': 'Cisco IOS-L3 Switch', 'Description': 'This is a Core-Switch', 'Lo0 IP Address': '10.1.1.1'}
请按照以下要求生成一个字典: 字典中的键为R1、R2、R3、R4等共计50个(R1....R50) 每个键对应的值为一个随机生成的IP地址。
import randomdict1 = {}for i in range(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'}
来源地址:https://blog.csdn.net/qinjilll/article/details/133826925
--结束END--
本文标题: 【Python】Python语言基础(中)
本文链接: https://lsjlt.com/news/430773.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0