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. ...
随机推荐
- tomcat出现的PermGen Space问题(bat,或者eclipse启动。)
参考地址 http://www.blogjava.net/allen-zhe/archive/2007/12/18/168556.html 参考地址:http://javavsxiaoming.ite ...
- nutch 1.7 修改代码后如何编译发布,并集群采集攻略
nutch 1.3之后,分布式的可执行文件与单机可执行文件进行了分离 接上篇,nutch 1.7 导入 eclipse 本篇所要解决的问题:nutch下载下来经过简单的配置即可进行采集,但有时候我们需 ...
- python判断用户注册中用户名是否包含非法字符
class UserRegisterForm(ModelForm): role = forms.IntegerField() check_password = forms.CharField(requ ...
- [BZOJ 1018] [SHOI2008] 堵塞的交通traffic 【线段树维护联通性】
题目链接:BZOJ - 1018 题目分析 这道题就说明了刷题少,比赛就容易跪..SDOI Round1 Day2 T3 就是与这道题类似的..然而我并没有做过这道题.. 这道题是线段树维护联通性的经 ...
- 横向技术分析C#、C++和Java优劣
转自横向技术分析C#.C++和Java优劣 C#诞生之日起,关于C#与Java之间的论战便此起彼伏,至今不辍.抛却Microsoft与Sun之间的恩怨与口角,客观地从技术上讲,C#与Java都是对传统 ...
- [wikioi]数字三角形
http://wikioi.com/problem/1220/ 最基本经典的DP题目,唯一有点意思的是,自底向上计算会更简洁.另外对这种+方式累计的DP,可以直接把原来的存进去,然后再加,本质是不用在 ...
- MyEclipse配置进行Hibernate逆映射
MyEclipse中配置MyEclipse Database Explorer 方法(以mysql 数据库为例) 前言: 之前看到同学转了一篇帖子,就是关于在MyEclipse中配置mysql的,今天 ...
- R语言 一元线性回归
#一元线性回归的基本步骤#1.载入数据 给出散点图 x<-c(0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.20,0.21,0.23) y< ...
- 8个超炫酷仿HTML5动画源码
1.jQuery万年历插件 带农历老皇历功能 这是一款基于jQuery的日历插件,这款日历插件和之前分享的日历控件有很大差异,它是一本万年历,包含了农历已经老皇历的功能,是一个挑好日子的工具.同时日历 ...
- IE浏览器兼容性的痛苦
做了一个弹出框的demo,在狐火,chrome,IE11中运行得好好的. 但是在IE8中死活不显示对话框,感觉IE8根本没有执行下面的javascript代码. 甚至,我简单的写alert(123), ...