Python字典及字典基本操作方法详解
|
本文实例讲述了Python字典及字典基本操作方法。分享给大家供大家参考,具体如下: 字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一冉ǖ挠成淅嘈停镜牟僮靼ㄈ缦拢 (1) 一、字典的创建 1.1 直接创建字典
d={'one':1,'two':2,'three':3}
print d
print d['two']
print d['three']
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.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:UsersMr_DengDesktoptest.py=======
items中的内容:
[('one',4)]
利用dict创建字典,输出字典内容:
{'four': 4,'three': 3,'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:UsersMr_DengDesktoptest.py=======
输出字典内容:
{'three': 3,'one': 1}
查询字典中的内容:
1
3
>>>
二、字典的格式化字符串
# _*_ coding:utf-8 _*_
d={'one':1,'three':3,'four':4}
print d
print "three is %(three)s." %d
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'four': 4,'one': 1}
three is 3.
>>>
三、字典方法 3.1 clear函数:清除字典中的所有项
# _*_ coding:utf-8 _*_
d={'one':1,'four':4}
print d
d.clear()
print d
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'four': 4,'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:UsersMr_DengDesktoptest.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:UsersMr_DengDesktoptest.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,'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:UsersMr_DengDesktoptest.py=======
初始X字典:
{'test': ['a','c'],'one': 1}
X复制到Y:
Y字典:
{'test': ['a','one': 1,'two': 2}
修改Y中的值,观察输出:
{'test': ['a','three': 33,'two': 2}
{'test': ['a','one': 1}
删除Y中的值,观察输出
{'test': ['a','b'],'one': 1}
>>>
注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改。
# _*_ coding:utf-8 _*_
from copy import deepcopy
x={}
x['test']=['a','c','d']
y=x.copy()
z=deepcopy(x)
print u'输出:'
print y
print z
print u'修改后输出:'
x['test'].append('e')
print y
print z
运算输出:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
输出:
{'test': ['a','d']}
{'test': ['a','d']}
修改后输出:
{'test': ['a','d','e']}
{'test': ['a','d']}
>>>
3.3 fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None # _*_ coding:utf-8 _*_ d=dict.fromkeys(['one','two','three']) print d 运算输出:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': None,'two': None,'one': None}
>>>
或者指定默认的对应值 # _*_ coding:utf-8 _*_ d=dict.fromkeys(['one','three'],'unknow') print d 运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 'unknow','two': 'unknow','one': 'unknow'}
>>>
3.4 get函数:访问字典成员
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
print d.get('one')
print d.get('four')
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
1
None
>>>
注:get函数可以访问字典中不存在的键,当该键不存在是返回None 3.5 has_key函数:检查字典中是否含有给出的键
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
print d.has_key('one')
print d.has_key('four')
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
True
False
>>>
3.6 items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
list=d.items()
for key,value in list:
print key,':',value
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
three : 3
two : 2
one : 1
>>>
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
it=d.iteritems()
for k,v in it:
print "d[%s]="%k,v
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
d[three]= 3
d[two]= 2
d[one]= 1
>>>
3.7 keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
print u'keys方法:'
list=d.keys()
print list
print u'niterkeys方法:'
it=d.iterkeys()
for x in it:
print x
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
keys方法:
['three','one']
iterkeys方法:
three
two
one
>>>
3.8 pop函数:删除字典中对应的键
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
d.pop('one')
print d
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
{'three': 3,'two': 2}
>>>
3.9 popitem函数:移出字典中的项
# _*_ coding:utf-8 _*_
d={'one':1,'three':3}
print d
d.popitem()
print d
运算结果:
=======RESTART: C:UsersMr_DengDesktoptest.py=======
{'three': 3,'one': 1}
{'two': 2,'one': 1}
>>>
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
