Python学习进程(10)字典
本节介绍Python中的字典:是另一种可变容器模型,且可存储任意类型对象。
(1)字典简介:
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
d = {key1 : value1, key2 : value2 }
注意:键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
(2)字典的操作:
包括创建字典、访问字典元素、修改字典的值、删除字典指定元素,删除字典:
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
#1.创建字典:
>>> dic={'name':'MenAngel','age':19,'sex':'男'};
>>> print dic
{'age': 19, 'name': 'MenAngel', 'sex': '\xc4\xd0'}
#2.修改字典中元素
>>> dic['name']='sunjimeng'
#3.访问字典中指定元素
>>> print dic
{'age': 19, 'name': 'sunjimeng', 'sex': '\xc4\xd0'}
>>> dic['name']
'sunjimeng'
>>> dic
{'age': 19, 'name': 'sunjimeng', 'sex': '\xc4\xd0'}
#4.删除字典中指定元素
>>> del dic['sex']
>>> dic
{'age': 19, 'name': 'sunjimeng'}
#5.删除字典中所有元素
>>> dic.clear()
>>> dic
{}
#6.删除字典
>>> del dic
(3)字典键的特性:
注意:字典值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的,但键不行。
1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住:
2)键必须不可变,所以可以用数字,字符串或元组充当,所以用列表就不行:
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dic={'name':'MenAngel','age':19,'name':'sunjimeng'}
>>> dic
{'age': 19, 'name': 'sunjimeng'}
#键可以用数字,字符串,可以用元组
>>> dic[4]=346
>>> dic
{'age': 19, 4: 346, 'name': 'sunjimeng'}
>>> dic[('abc',20)]='sex'
>>> dic
{('abc', 20): 'sex', 'age': 19, 4: 346, 'name': 'sunjimeng'}
#但不能用列表
>>> dic[[20,]]=20
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
(4)字典内置函数或方法:
1)字典内置函数:

#1.第一种字典初始化方式:
>>> dic1={}
>>> dic1['name']='MenAngel';
>>> dic1['age']=20;
>>> dic1['sex']='男';
#2.第二种字典初始化方式
>>> dic2={'name':'MenAngel','age':20,'sex':'男']
>>> dic1
{'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'}
>>> dic2
{'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'}
>>> cmp(dic1,dic2)
0
>>> len(dic1)
3
>>> str(dic1)
"{'age': 20, 'name': 'MenAngel', 'sex': '\\xc4\\xd0'}"
>>> type(dic1)
<type 'dict'>
2)Python字典的内置方法:

>>> dict1={'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'}
>>> dict2=dict1.copy()
>>> list1=dict1.keys()
>>> list2=dict1.values()
>>> dict1.has_key('name')
True
>>> dict1,dict2
({'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'}, {'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'})
>>> list1,list2
(['age', 'name', 'sex'], [20, 'MenAngel', '\xc4\xd0'])
>>> dict1['name']='sunjimeng'
>>> dict1,dict2
({'age': 20, 'name': 'sunjimeng', 'sex': '\xc4\xd0'}, {'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'})
>>> dict1.update(dict2)
>>> dict1,dict2
({'name': 'MenAngel', 'age': 20, 'sex': '\xc4\xd0'}, {'age': 20, 'name': 'MenAngel', 'sex': '\xc4\xd0'})
>>> dict.setdefault('abc','')
20
>>> dict1.setdefault('abc','')
''
>>> dict1.setdefault('abc','')
''
>>> dict1.items()
[('abc', ''), ('name', 'MenAngel'), ('age', 20), ('sex', '\xc4\xd0')]
Python学习进程(10)字典的更多相关文章
- Python学习进程
1周第1天 主要是变量的学习(11月8日) 1.1 python安装(win和linux下)1.2 ipython安装及使用1.3 变量的定义1.4 变量赋值1.5 运算符(赋值.算术.关系.逻辑)1 ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- Python学习4(字典的内存分布)
1.字典:是python数据类型之一,字典通过花括号来包含数据项,字典的每个元素由2个部分组成,键:值,字典是根据键来找对应的值. data = {"name": "Et ...
- python学习(10)字典学习,写一个三级菜单程序
学习了字典的应用.按老师的要求写一个三级菜单程序. 三级菜单程序需求如下: 1.深圳市的区--街道--社区---小区4级 2.建立一个字典,把各级区域都装进字典里 3.用户可以从1级进入2级再进入3级 ...
- python 学习笔记 10 -- 正則表達式
零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...
- Python学习进程(12)模块
模块让你能够有逻辑地组织你的Python代码段. (1)python模块: 模块化的好处: 1.把相关的代码分配到一个模块里能让你的代码更好用,更易懂. 2.模块也是Python对象, ...
- Python学习进程(3)Python基本数据类型
本节介绍在Python语法中不同的变量数据类型. (1)基本数据类型: >>> a=10; >>> b=10.0; >>> c=T ...
- Python学习笔记之字典
一.创建和使用字典 1.创建字典 phonebook={'Alice':'2341','Beth':'9102','Cecil':'3258'} 2.dict,通过映射创建字典 >>> ...
- python学习笔记10(Python的内存管理)
用这张图激励一下自己,身边也就只有一位全栈数据工程师!!! 32. Python的内存管理 1. 对象的内存使用 对于整型和短字符串对象,一般内存中只有一个存储,多次引用.其他的长字符串和其他对象 ...
随机推荐
- 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。
/** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...
- Asteroids - poj 3041(二分图最大匹配问题)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17258 Accepted: 9386 Description Be ...
- 多媒体开发之---h264格式详解
http://blog.csdn.net/bluebirdssh/article/details/6533501 http://blog.csdn.net/d_l_u_f/article/detail ...
- poj 1470(LCA)
题目链接:http://poj.org/problem?id=1470 思路:题目的意思很简单,就是求树中每个节点作为某两个节点的最近公共祖先的次数,这里我们可以用sum数组来保存,然后就是从根节点开 ...
- PID file found but no matching process was found. Stop aborted
一般脚本部署时不会遇到这种情况,有时候自个手动处理会出现”PID file found but no matching process was found. Stop aborted”,根据意思就可以 ...
- LeetCode Problem 35:Search Insert Position
描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 回溯法——n后问题
问题描述: 在n*n的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于在n*n格的棋盘上放置n个皇后,任何2个皇后不放在同一行 ...
- ZOJ1119(SPF)
题目链接:传送门 题目大意:一副无向图,问有多少个节点满足删除该节点后图不连通,对于每个满足条件的节点,输出节点编号及删除节点将图分为几个连通块.若没有节点满足则输出No SPF nodes 题目思路 ...
- c++获取读写文本权限
#include<cstdio> #include<iostream> #include<fstream> using namespace std; int tot ...
- 《从零开始学Swift》学习笔记(Day 17)——Swift中数组集合
原创文章,欢迎转载.转载请注明:关东升的博客 数组(Array)是一串有序的由相同类型元素构成的集合.数组中的集合元素是有序的,可以重复出现. 声明一个Array类型的时候可以使用下面的语句之一. v ...