python-字典(第二篇(四):字典)
【Python之旅】第二篇(四):字典
摘要: 说明: 显然Python中字典的学习过程与列表是一样的,主要是围绕下面的函数来进行重点学习: 1 2 3 4 5 6 7 8 9 10 11 >>> xpleaf. xpleaf.clear( xpleaf.copy( xpleaf.get( xpleaf.has_key(...
说明:
显然Python中字典的学习过程与列表是一样的,主要是围绕下面的函数来进行重点学习:
|
1
2
3
4
5
6
7
8
9
10
11
|
>>> xpleaf.xpleaf.clear(xpleaf.copy(xpleaf.get(xpleaf.has_key(xpleaf.items(xpleaf.keys(xpleaf.pop(xpleaf.popitem(xpleaf.setdefault(xpleaf.update( |
1.基本操作
--创建一个字典
|
1
2
3
4
5
6
7
8
|
>>> xpleaf = {... 'name':'xpleaf',... 'occupation':'student',... 'hobby':'computer',... 'dream':'excellent hacker'... }>>> xpleaf{'hobby': 'computer', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
·容易观察到字典的输出并没有按照创建字典时的顺序进行输出,因为字典按哈希值查找内容,而不是按索引号;
·{key:value}是字典的基本语法格式,key是唯一的,value可为大多数数据类型;
--查看键值对应的内容
|
1
2
|
>>> xpleaf['hobby']'computer' |
--修改键值对应的内容
|
1
2
3
|
>>> xpleaf['hobby'] = 'IT'>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
--添加一个键值对
|
1
2
3
|
>>> xpleaf['girlfriend'] = 'none'>>> xpleaf{'girlfriend': 'none', 'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
·添加的元素在字典中的排序是随机的,因为索引号对字典没有意义(按照哈希值进行value值的查找);
2.has_key()函数
·功能:接受key的查询,以bool值形式返回查询字典中是否有该key;
·演示如下:
|
1
2
3
4
|
>>> xpleaf.has_key('dream')True>>> xpleaf.has_key('wife')False |
3.items()函数
·功能:将字典转换为列表,列表的元素为元组,其中左元素为key,右元素为value;
·演示如下:
|
1
2
|
>>> xpleaf.items()[('girlfriend', 'none'), ('hobby', 'IT'), ('dream', 'excellent hacker'), ('name', 'xpleaf'), ('occupation', 'student')] |
·基于上述输出形式,可对字典的key和value进行遍历,如下:
|
1
2
3
4
5
6
7
8
|
>>> for key,value in xpleaf.items():... print key,value... girlfriend nonehobby ITdream excellent hackername xpleafoccupation student |
·item()函数的原理是把字典转换为列表存储在内存中,对于数据量大的情况下,会比较慢;
·大数据量的字典遍历方法:
|
1
2
3
4
5
6
7
8
|
>>> for key in xpleaf:... print key,xpleaf[key]... girlfriend nonehobby ITdream excellent hackername xpleafoccupation student |
4.get()函数
·功能:取对应key的value值;
·演示如下:
|
1
2
3
4
5
6
|
>>> xpleaf{'girlfriend': 'none', 'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.get('dream')'excellent hacker'>>> xpleaf.get('wife') ===>如果没有该key值则不会有输出>>> |
·即相当于dict[key]的方法取value值;
5.keys()函数
·功能:取出字典中的key值,并生成相应的列表;
·演示如下:
|
1
2
|
>>> xpleaf.keys()['girlfriend', 'hobby', 'dream', 'name', 'occupation'] |
5.pop()函数
·功能:弹出一个key,即删除一个键值对;
·演示如下:
|
1
2
3
4
5
6
|
>>> xpleaf{'girlfriend': 'none', 'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.pop('girlfriend')'none'>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
6.popitem()函数
·功能:按顺序删除字典中的元素;
·演示如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
>>> a{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 6: 'f'}>>> a.popitem()('a', 1)>>> a.popitem()('c', 3)>>> a.popitem()('b', 2)>>> a.popitem()('e', 5)>>> a.popitem()('d', 4)>>> a.popitem()(6, 'f') |
7.setdefault()函数
·在字典中添加元素,如果原来存在该元素,则不进行任何修改;
·演示如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.setdefault('hobby','computer') ===>'hobby'键值对已经存在,不会添加'IT'>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.setdefault('weight','55kg') ===>'weight'键值对不存在,会进行添加'55kg'>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf.setdefault('wife') ===>添加没有的键值对,>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'} |
8.update()函数
·功能:合并两个字典
·演示如下:
|
1
2
3
4
5
6
7
8
9
|
>>> a{'a': 1, 'c': 3, 'b': 2}>>> b{'e': 4, 'g': 6, 'f': 5}>>> a.update(b)>>> a{'a': 1, 'c': 3, 'b': 2, 'e': 4, 'g': 6, 'f': 5}>>> b{'e': 4, 'g': 6, 'f': 5} |
·合并的顺序依然是随机的,原理与前面一样;
·更新的只是字典a,字典b没有变化;
·如果合并字典时有重复的item项,则会进行覆盖:
|
1
2
3
4
5
6
7
|
>>> a{'a': 1, 'c': 3, 'b': 2, 'e': 4, 'g': 6, 'f': 5}>>> c{'b': 'cover2', 'g': 'cover1'}>>> a.update(c)>>> a{'a': 1, 'c': 3, 'b': 'cover2', 'e': 4, 'g': 'cover1', 'f': 5} |
9.values()函数
·功能:取字典中所有key的value值,并生成相应的列表
·演示如下:
|
1
2
3
4
|
>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf.values()['xpleaf', '55kg', None, 'IT', 'excellent hacker', 'student'] |
·多用在value值的数据类型都相同的字典中,以用于数据的批量分析;
10.clear()函数
·功能:清空字典的item项
·演示如下:
|
1
2
3
4
5
|
>>> a{'a': 1, 'c': 3, 'b': 'cover2', 'e': 4, 'g': 'cover1', 'f': 5}>>> a.clear()>>> a{} |
·与del不同,del是直接删除字典:
|
1
2
3
4
5
|
>>> del a>>> aTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'a' is not defined |
11.copy()函数
·功能:对字典进行浅复制;
·Python中普通情况下的“复制”:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf_copy = xpleaf>>> xpleaf_copy{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf['hobby'] = 'IT_Field'>>> xpleaf_copy{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf_copy['wife'] = 'None!!!'>>> xpleaf_copy{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'} |
·即将变量赋给其它变量只是将对象(实际的字典)作一个引用传递而已,修改任何一个引用都会改变原来对象的值;
·copy()的浅复制功能则不是引用传递:
|
1
2
3
4
5
6
7
8
|
>>> xpleaf_copy2 = xpleaf.copy()>>> xpleaf_copy2{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf_copy2['wife'] = 'CL'>>> xpleaf_copy2{'name': 'xpleaf', 'weight': '55kg', 'wife': 'CL', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'} |
·当然copy()更重要的作用不仅在于此,这里只是简单提及它的作用。
python-字典(第二篇(四):字典)的更多相关文章
- Python人工智能第二篇:人脸检测和图像识别
Python人工智能第二篇:人脸检测和图像识别 人脸检测 详细内容请看技术文档:https://ai.baidu.com/docs#/Face-Python-SDK/top from aip impo ...
- Python【第二篇】运算符及优先级、数据类型及常用操作、深浅拷贝
一.运算符及优先级 Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 1.算数运算符 运算符 描述 实例,a=20,b=10 + 加 a+b输出结果30 - 减 a-b输出结果 ...
- Python人工智能第二篇
Python人工智能之路 - 第二篇 : 现成的技术 预备资料: 1.FFmpeg: 链接:https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg 密码:w ...
- Python开发第二篇
运算符 1.算术运算符 % 取余运算符,返回余数 ** 幂运算符 //返回商的整数部分 2.逻辑运算符 and 与运算符 a and b 如果a为False是,表达式为False,如果a为True返 ...
- python 【第二篇】python基本数据类型
python数据类型 python的数据类型和大多数编程语言一样,有int,float,long,string但是python有三个特殊的数据类型:列表,元组,字典 如果不知道你的数据类型有什么方法: ...
- python【第二篇】列表、元组、字典及文件操作
本节内容 列表 元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作:列表有序.可变.元素 ...
- Python强化训练笔记(四)——字典的排序
假如有学生成绩以字典顺序排列:{'Tom': 87, 'Jack': 90, 'Rose': 100.....} 想要根据学生的成绩来进行排序,可以考虑使用sorted函数.但是sorted函数用在字 ...
- 【python自动化第二篇:python入门】
内容概览 模块 python运行过程 基本数据类型(数字,字符串) 序列类型(列表,元组,字典) 模块使用 模块我们可以把它想象成导入到python以增强其功能的一种拓展.需要使用import来导入模 ...
- [Python笔记]第二篇:运算符、基本数据类型
本篇主要内容有:运算符 基本数据类型等 一.运算符 1.算术运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 6.身份运算 7.位运算 8.运算符优先级 二.基本数据类型 1.整数:int ...
- 图解Python 【第二篇】:Python基础2
本节内容一览图 一.数据类型 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4. ...
随机推荐
- matlab函数之bsxfun
bsxfun(fun,A,B) 偶然间发现了这个函数,强大得不得了呀,它的作用是:对两个矩阵A和B之间的每一个元素进行指定的计算(函数fun指定):并且具有自动扩维的作用 例如,A是一个4*3的矩阵, ...
- initWithCoder: 与initWithFrame:
之前一直用代码来编写画面,现在着手使用storyboard和xib来构筑画面,遇到initWithCoder方法, 故查了下,initWithCoder方法的调用,看了篇博客,链接如下: http:/ ...
- jQuery中的data方法:
向元素附加数据,然后取回该数据: $("#btn1").click(function(){ $("div").data("greeting" ...
- javascript for
var k = ["a", "b", "c", false]; for(var i = 0, m; m = k[i]; i++) { con ...
- bootcamp
为了鄙社自主研发的html5studio和mist,我给Air划了32G装windows囧 第一要注意的是,必须使用bootcamp划分将要安装windows的分区,不要在windows安装过程中删除 ...
- c++ 名字粉碎(name mangling)
转自Ibm: Name mangling is the encoding of function and variable names into unique names so that linker ...
- BZOJ1606: [Usaco2008 Dec]Hay For Sale 购买干草
1606: [Usaco2008 Dec]Hay For Sale 购买干草 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 612 Solved: 46 ...
- 最全面的 DNS 原理入门
DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...
- hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- poj3693 Maximum repetition substring
题意 给出一个长度为\(n(n\leqslant 100000)\)的串,求一个字典序最小的子串使得它是某个字符串重复\(k\)次得到的,且\(k\)最大 题解 后缀数组论文上的题,跟上一篇uva那个 ...