字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一內建的映射类型,基本的操作包括如下: 
(1)len():返回字典中键—值对的数量; 
(2)d[k]:返回关键字对于的值; 
(3)d[k]=v:将值关联到键值k上; 
(4)del d[k]:删除键值为k的项; 
(5)key in d:键值key是否在d中,是返回True,否则返回False。

一、字典的创建 
1.1 直接创建字典

d={'one':1,'two':2,'three':3}
print d
print d['two']
print d['three']
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
2
3

1.2 通过dict创建字典

# _*_ coding:utf-8 _*_
items=[('one',1),('two',2),('three',3),('four',4)]
print u'items中的内容:'
print items
print u'利用dict创建字典,输出字典内容:'
d=dict(items)
print d
print u'查询字典中的内容:'
print d['one']
print d['three']
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
items中的内容:
[('one', 1), ('two', 2), ('three', 3), ('four', 4)]
利用dict创建字典,输出字典内容:
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
查询字典中的内容:
1
3 或者通过关键字创建字典
# _*_ coding:utf-8 _*_
d=dict(one=1,two=2,three=3)
print u'输出字典内容:'
print d
print u'查询字典中的内容:'
print d['one']
print d['three']

运算结果: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py======= 输出字典内容:

{'three': 3, 'two': 2, 'one': 1}

查询字典中的内容:

1

3

二、字典的格式化字符串

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3,'four':4}
print d
print "three is %(three)s." %d

运算结果: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four': 4, 'three': 3, 'two': 2, 'one': 1}

three is 3.

三、字典方法 
3.1 clear函数:清除字典中的所有项

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3,'four':4}
print d
d.clear()
print d

运算结果: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four': 4, 'three': 3, 'two': 2, 'one': 1}

{}

请看下面两个例子 
3.1.1

# _*_ coding:utf-8 _*_
d={}
dd=d
d['one']=1
d['two']=2
print dd
d={}
print d
print dd

运算结果: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two': 2, 'one': 1}

{}

{'two': 2, 'one': 1}

3.1.2

# _*_ coding:utf-8 _*_
d={}
dd=d
d['one']=1
d['two']=2
print dd
d.clear()
print d
print dd

运算结果: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two': 2, 'one': 1}

{}

{}

3.1.2与3.1.1唯一不同的是在对字典d的清空处理上,3.1.1将d关联到一个新的空字典上,这种方式对字典dd是没有影响的,所以在字典d被置空后,字典dd里面的值仍旧没有变化。但是在3.1.2中clear方法清空字典d中的内容,clear是一个原地操作的方法,使得d中的内容全部被置空,这样dd所指向的空间也被置空。 
3.2 copy函数:返回一个具有相同键值的新字典

# _*_ coding:utf-8 _*_
x={'one':1,'two':2,'three':3,'test':['a','b','c']}
print u'初始X字典:'
print x
print u'X复制到Y:'
y=x.copy()
print u'Y字典:'
print y
y['three']=33
print u'修改Y中的值,观察输出:'
print y
print x
print u'删除Y中的值,观察输出'
y['test'].remove('c')
print y
print x

运算结果: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

初始X字典: {'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}

X复制到Y: Y字典: {'test': ['a', 'b', 'c'], 'one': 1, 'three': 3, 'two': 2}

修改Y中的值,观察输出:

{'test': ['a', 'b', 'c'], 'one': 1, 'three': 33, 'two': 2}

{'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}

删除Y中的值,观察输出

{'test': ['a', 'b'], 'one': 1, 'three': 33, 'two': 2}

{'test': ['a', 'b'], 'three': 3, 'two': 2, 'one': 1}

注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改。deepcopy函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题。

# _*_ coding:utf-8 _*_
from copy import deepcopy
x={}
x['test']=['a','b','c','d']
y=x.copy()
z=deepcopy(x)
print u'输出:'
print y
print z
print u'修改后输出:'
x['test'].append('e')
print y
print z

运算输出:

输出:

{'test': ['a', 'b', 'c', 'd']}

{'test': ['a', 'b', 'c', 'd']}

修改后输出: =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'test': ['a', 'b', 'c', 'd', 'e']}

{'test': ['a', 'b', 'c', 'd']}

3.3 fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None

# _*_ coding:utf-8 _*_
d=dict.fromkeys(['one','two','three'])
print d

运算输出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': None, 'two': None, 'one': None}

或者指定默认的对应值

# _*_ coding:utf-8 _*_
d=dict.fromkeys(['one','two','three'],'unknow')
print d

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 'unknow', 'two': 'unknow', 'one': 'unknow'}

3.4 get函数:访问字典成员

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print d.get('one')
print d.get('four')

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 1}

1

None

注:get函数可以访问字典中不存在的键,当该键不存在是返回None 
3.5 has_key函数:检查字典中是否含有给出的键

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print d.has_key('one')
print d.has_key('four')

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 1}

True

False

3.6 items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
list=d.items()
for key,value in list:
print key,':',value

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 1}

three : 3

two : 2

one : 1

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
it=d.iteritems()
for k,v in it:
print "d[%s]="%k,v

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 1}

d[three]= 3

d[two]= 2

d[one]= 1

3.7 keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print u'keys方法:'
list=d.keys()
print list
print u'\niterkeys方法:'
it=d.iterkeys()
for x in it:
print x

运算结果:

{'three': 3, 'two': 2, 'one': 1}

keys方法:

['three', 'two', 'one']

iterkeys方法:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

three

two

one

3.8 pop函数:删除字典中对应的键

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
d.pop('one')
print d

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 1}

{'three': 3, 'two': 2}

3.9 popitem函数:移出字典中的项

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
d.popitem()
print d

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 1}

{'two': 2, 'one': 1}

3.10 setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值

# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print d.setdefault('one',1)
print d.setdefault('four',4)
print d

运算结果:

{'three': 3, 'two': 2, 'one': 1}

1

4

{'four': 4, 'three': 3, 'two': 2, 'one': 1}

3.11 update函数:用一个字典更新另外一个字典

# _*_ coding:utf-8 _*_
d={
'one':123,
'two':2,
'three':3
}
print d
x={'one':1}
d.update(x)
print d

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three': 3, 'two': 2, 'one': 123}

{'three': 3, 'two': 2, 'one': 1}

3.12 values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素

# _*_ coding:utf-8 _*_
d={
'one':123,
'two':2,
'three':3,
'test':2
}
print d.values()

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

[2, 3, 2, 123]

from:http://blog.csdn.net/u010480899/article/details/52737739

Python——字典与字典方法的更多相关文章

  1. python 字典内置方法get应用

    python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...

  2. Python中多个列表与字典的合并方法

    Python中多个列表与字典的合并方法 1多列表的合并 1)a+=b a=['] b = ['] a += b print(a) >>>['] 2) a.extend(b) a=[' ...

  3. python 3.x 字典的11种方法

    python 3.x 字典的11种方法2017年11月25日 01:02:11 Milton-Long 阅读数:535 标签: python python字典方法 更多个人分类: python-学习之 ...

  4. 遍历python字典几种方法

    遍历python字典几种方法 from: http://ghostfromheaven.iteye.com/blog/1549441 aDict = {'key1':'value1', 'key2': ...

  5. python 字典(dict)get方法应用

    如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这里我们可以用字典做一个小游戏,假设用 ...

  6. Python简单遍历字典及删除元素的方法

    Python简单遍历字典及删除元素的方法 这篇文章主要介绍了Python简单遍历字典及删除元素的方法,结合实例形式分析了Python遍历字典删除元素的操作方法与相关注意事项,需要的朋友可以参考下 具体 ...

  7. Python中使用item()方法遍历字典的例子

    Python中使用item()方法遍历字典的例子 这篇文章主要介绍了Python中使用item()方法遍历字典的例子,for...in这种是Python中最常用的遍历字典的方法了,需要的朋友可以参考下 ...

  8. Python 字典(Dictionary) clear()方法

    Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...

  9. Python 字典(Dictionary) type()方法

    Python 字典(Dictionary) type()方法 描述 Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型.高佣联盟 www.c ...

  10. Python 字典(Dictionary) str()方法

    Python 字典(Dictionary) str()方法 描述 Python 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示.高佣联盟 www.cge ...

随机推荐

  1. Oracle删除主键约束的同时删除索引

    继续昨天的折腾(Oracle修改主键约束),删掉主键约束后,发现唯一索引并未删掉.仔细看了下,主键约束跟唯一索引名称不一样,这说明是先创建了唯一索引,后创建的主键约束.我们来试验下: SQL> ...

  2. 轻量级封装DbUtils&Mybatis之二Dbutils

    DbUtils入门 Apache出品的极为轻量级的Jdbc访问框架,核心类只有两个:QueryRunner和ResultSetHandler. 各类ResultSetHandler: ArrayHan ...

  3. Unit01: Spring简介 、 Spring容器 、 Spring IOC

    Unit01: Spring简介 . Spring容器 . Spring IOC Spring (1)Spring是什么? Spring是一个开源的用来简化应用开发的框架. (2)Spring的特点? ...

  4. CentOS 7 named设置主从复制

    前两篇文章介绍了named的安装和配置forward. 本文将介绍named的主从复制. 在从named的配置中添加: zone "weiheng.ink" IN { type s ...

  5. c#实现QQ群成员列表导出及邮件群发之群列表及群成员获取

    主题已迁移至:http://atiblogs.com/ ITO-神奇的程序员

  6. css 字体英文对照

    宋体: SimSun 黑体: SimHei 华文细黑: STHeiti Light [STXihei] 华文黑体: STHeiti 微软雅黑: Microsoft YaHei 微软正黑体: Micro ...

  7. nginx web服务优化

    nginx基本安全优化 1. 调整参数隐藏nginx软件版本号信息 软件的漏洞和版本有关,我们应尽量隐藏或消除web服务对访问用户显示各类敏感信息(例如web软件名称及版本号等信息),这样恶意的用户就 ...

  8. 第十章 Secret & Configmap(下)

    10.4 ConfigMap Secret可以为Pod提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如一些配置信息,则可以用ConfigMap. configMap的使用方式与Secre ...

  9. 【BZOJ】2342: [Shoi2011]双倍回文(Manacher)

    题目 传送门:QWQ 分析 (sb如我写了发不知道什么东西在洛谷上竟然水了84分 嗯咳 设$ i $为双重回文的中心 如果$ j~i $ 可以被算作答案,只有满足如下两式: $ p[j]+j \geq ...

  10. 多个else if语句

    public class demo { public static void main(String[] args) { boolean examIsDone = true; int score = ...