[Python Study Notes]字典操作

字典操作

a.增加

1 >>> info["stu1104"] = "abc"
2 >>> info
3 {‘stu1102‘: ‘x5456‘, ‘stu1104‘: ‘abc‘, ‘stu1103‘: ‘Mali‘, ‘stu1101‘: ‘Wu‘}

b.修改

1 >>> info[‘stu1101‘] = "xinge"
2 >>> info
3 {‘stu1102‘: ‘x5456‘, ‘stu1103‘:  Mali‘, ‘stu1101‘: ‘xinge‘}

c.删除

 1 >>> info
 2 {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1101‘: ‘武藤兰‘}
 3 >>> info.pop("stu1101") #标准删除姿势
 4 ‘武藤兰‘
 5 >>> info
 6 {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘}
 7 >>> del info[‘stu1103‘] #换个姿势删除
 8 >>> info
 9 {‘stu1102‘: ‘LongZe Luola‘}
10 >>>
11 >>>
12 >>>
13 >>> info = {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘}
14 >>> info
15 {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘} #随机删除
16 >>> info.popitem()
17 (‘stu1102‘, ‘LongZe Luola‘)
18 >>> info
19 {‘stu1103‘: ‘XiaoZe Maliya‘}

d.查找

 1 >>> info = {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘}
 2 >>>
 3 >>> "stu1102" in info #标准用法
 4 True
 5 >>> info.get("stu1102")  #获取
 6 ‘LongZe Luola‘
 7 >>> info["stu1102"] #同上,但是看下面
 8 ‘LongZe Luola‘
 9 >>> info["stu1105"]  #如果一个key不存在,就报错,get不会,不存在只返回None
10 Traceback (most recent call last):
11   File "<stdin>", line 1, in <module>
12 KeyError: ‘stu1105‘

e.获取键值

1 #values
2 >>> info.values()
3 dict_values([‘LongZe Luola‘, ‘XiaoZe Maliya‘])
4
5 #keys
6 >>> info.keys()
7 dict_keys([‘stu1102‘, ‘stu1103‘])

f.合并

1 #update
2 >>> info
3 {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1106‘: ‘Alex‘}
4 >>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
5 >>> info.update(b)
6 >>> info
7 {‘stu1102‘: ‘龙泽萝拉‘, 1: 2, 3: 4, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1106‘: ‘Alex‘}

g.深copy

1 import copy
2    
3 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
4    
5 n4 = copy.deepcopy(n1)

h.循环

1 #方法1
2 for key in info:
3     print(key,info[key])
4  
5 #方法2
6 for k,v in info.items(): #会先把dict转成list,数据里大时莫用
7     print(k,v)

原文地址:https://www.cnblogs.com/liu66blog/p/8251425.html

时间: 2024-10-10 18:43:02

[Python Study Notes]字典操作的相关文章

[Python Study Notes]文件操作

文件操作 对文件操作流程 打开文件,可添加filepath打开某绝对路径下的文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 1 # The_author = 'liu66' 2 # -*- coding = utf-8 -*- 3 4 filepath='D:\学习资料\ehmatthes-pcc-6bfeca0\chapter_10\pi_digits.txt' 5 6 read_sting = '' 7 8 with open(filepath) as file_obj

[Python Study Notes]字符串操作

字符串操作 a.字符串格式化输出 1 name = "liu" 2 print "i am %s " % name 3     4 #输出: i am liu   6 PS: 字符串是 %s;整数 %d;浮点数%f b.查找字符串(find) 1 str.find(str, start=0, end=len(mystr)) # 检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1 1 >>> str = 'xinge 好帅'

[Python Study Notes]字符串处理技巧(持续更新)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 字符串处理.py >>作者: liu yang >>邮箱: [email protected] >>博客: www.cnblogs.com/liu66blog '''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]磁盘分区和io性能

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 磁盘信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]内存信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 内存信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]进程信息(丁丁软件监控进程,http-post)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 进程信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]CS架构远程访问获取信息--Client端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]CS架构远程访问获取信息--Client端v1.0

更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''

[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0

更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_server.py >>作者: liu yang >>邮箱: [email protected] >&g