python 字典相关操作.docx
- 文档编号:7279909
- 上传时间:2023-05-11
- 格式:DOCX
- 页数:10
- 大小:17.52KB
python 字典相关操作.docx
《python 字典相关操作.docx》由会员分享,可在线阅读,更多相关《python 字典相关操作.docx(10页珍藏版)》请在冰点文库上搜索。
python字典相关操作
Python中的一种数据结构——字典(Dictionary)的使用方法总结。
一、dictionary数据类型的结构是:
{key1:
value1,key2:
value2,...},即键值对。
字典的健必须是不可更改的类型,如字符串、数字、元祖等;而值则可以是任意的数据类型,而且同一个字典当中可以混用数据类型,如:
?
1
d={'a':
1,'b':
[1,2,3],'c':
('hello','world')}
字典的key不可以重复。
需要注意的一点是:
字典是一个无序存储结构。
二、新建字典
新建一个空字典,例:
d={}
新建一个非空字典,例:
d={'a':
'hello','b':
'world'}
使用工厂函数dict,传入一个元素是列表的元祖作为参数来创建字典,例:
?
1
2
d=dict((['a','hello'],['b','world']))
#返回{'a':
'hello','b':
'world'}
还有一种使用dict函数创建字典的方法,例:
d=dict(x=1,y=2),返回d为{'x':
1,'y':
2}
使用字典对象本身的fromkeys(s,[v])方法来创建一个字典,字典的健来自参数s,所有的值均为参数v的值,若参数v未给出则默认均为None,例:
?
1
2
d={}.fromkeys(('a','b'),1)
#返回{'a':
1,'b':
1}
三、访问字典
1.通过健来访问值,例:
d[key]。
可以通过key来引用value,但不可以通过value来引用key。
读取不存在的key会引发异常,对不存在的key做赋值操作则会为字典增加一对键值。
2.遍历字典:
forkeyind.keys()或者可以直接forkeyind来操作。
3.d.keys()——返回一个包含所有键的list,需要注意该list并不按照字典定义的顺序输出。
4.d.values()——返回一个包含所有值的list,值列表的元素顺序与键列表的顺序一致。
5.d.items()——返回一个list,该list由形如(key,value)的tuple组成。
6.d.copy——得到字典的一个浅拷贝。
7.d.get(key)——若字典中有key则返回d[key],否则返回None。
8.d.get(key,default)——同上,若无则返回default的值。
9.d.has_key(key)——判断是否含有key,含有则返回True,否则False。
10.判断字典中是否存在某个键,还可以用"in","notin"操作符。
四、字典的增删改
1.增加字典元素——d['a']='hello'。
2.删除字典元素——deld['a']或者d.pop('a')。
3.清空字典元素——d.clear()从一个字典中清除所有元素。
4.删除字典对象——deld
5.把字典a的元素加入到字典d当中,键重复时会覆盖字典d的键值——d.update(a)。
6.依顺序得到字典的一个键值对并删除,若字典是空的则抛出异常——d.popitem()
7.删除指定键的键值对,并返回该键对应的值,若键不存在则返回参数v——d.pop(key,[v])
增加字典元素
>>>nameinfo={}
>>>nameinfo['a1']='yangrong' #若字典里有a1主键,则覆盖原来的值,没有,则添加
>>>nameinfo
{'a1':
'yangrong'}
遍历字典主键与键值
>>>fork,valueinnameinfo.items():
... printk,value
...
a1yangrong
查看字典所有主键
>>>dict={'ob1':
'computer','ob2':
'mouse','ob3':
'printer'}
>>>
>>>
>>>dict.keys()
['ob2','ob3','ob1']
判断字典中是否有该主键
>>>dict.keys()
['ob2','ob3','ob1']
>>>dict.has_key('ob2') #或'ob2'indict
True
>>>dict.has_key('ob4')
False
也有人用循环方法来判断
forkeyindict.keys():
但是这种方法毕竟不够简洁,
查看字典所有键值内容
>>>dict={'ob1':
'computer','ob2':
'mouse','ob3':
'printer'}
>>>dict.values()
['mouse','printer','computer']
列出所有项目
>>>dict.items()
[('ob2','mouse'),('ob3','printer'),('ob1','computer')]
清空字典
>>>dict.clear()
>>>dict
{}
拷贝字典
>>>dict
{'ob2':
'mouse','ob3':
'printer','ob1':
'computer'}
>>>a=dict
>>>a
{'ob2':
'mouse','ob3':
'printer','ob1':
'computer'}
>>>b=dict.copy()
>>>b
{'ob2':
'mouse','ob3':
'printer','ob1':
'computer'}
比较字典
>>>cmp(a,b)
首先比较主键长度,然后比较键大小,然后比较键值大小,(第一个大返回1,小返回-1,一样返回0)
更新字典
>>>dict={'yangrong':
{"age":
"23","job":
"IT"},"xiaohei":
{"'age':
'22','job':
'dota'"}}
>>>dict
{'xiaohei':
set(["'age':
'22','job':
'dota'"]),'yangrong':
{'age':
'23','job':
'IT'}}
>>>dict['xiaohei']=111 #修改一级字典
>>>dict
{'xiaohei':
111,'yangrong':
{'age':
'23','job':
'IT'}}
>>>dict['yangrong']['age']=25 #修改二级字典
>>>dict
{'xiaohei':
111,'yangrong':
{'age':
25,'job':
'IT'}}
>>>dict={'yangrong':
['23','IT'],"xiaohei":
['22','dota']}
>>>dict['xiaohei'][1]="dota2" #修改字典中列表某项,1是代表列表中第2个字符串。
>>>dict
{'xiaohei':
['22','dota2'],'yangrong':
['23','IT']}
删除字典元素
>>>dict
{'xiaohei':
['22','dota2'],'yangrong':
['23','IT']}
>>>deldict['xiaohei'] #删除xiaohei键值
>>>dict
{'yangrong':
['23','IT']}
>>>dict
{'yangrong':
['23','IT']}
>>>
>>>deldict['yangrong'][1] #删除yangrong主键的每2字值
>>>dict
{'yangrong':
['23']}
删除整个字典
>>>dict
{'yangrong':
['23']}
>>>dict.clear() #同deldict
>>>dict
{}
将字符串切分为列表
>>>s="helloworldbye"
>>>s.split() #用于把有规律的文本,读取出来后,使用列表进行修改,再写入文件。
['hello','world','bye']
将列表转换为字符串
S.split(str,'') #将string转list,以空格切分
存储字典(pickle序列化)
#需导入pickle模块 importpickle
把字典内容存入文件
f=file('data.txt','wb') #新建文件data.txt,'wb',b是打开块文件,对于设备文件有用
pickle.dump(a,f) #把a序列化存入文件
f.close()
把内容读入字典(反序列化)
a=open('data.txt','rb') #打开文件
printpickle.load(a) #把内容全部反序列化
增加字典元素
>>>nameinfo={}
>>>nameinfo['a1']='yangrong' #若字典里有a1主键,则覆盖原来的值,没有,则添加
>>>nameinfo
{'a1':
'yangrong'}
遍历字典主键与键值
>>>fork,valueinnameinfo.items():
... printk,value
...
a1yangrong
查看字典所有主键
>>>dict={'ob1':
'computer','ob2':
'mouse','ob3':
'printer'}
>>>
>>>
>>>dict.keys()
['ob2','ob3','ob1']
判断字典中是否有该主键
>>>dict.keys()
['ob2','ob3','ob1']
>>>dict.has_key('ob2') #或'ob2'indict
True
>>>dict.has_key('ob4')
False
也有人用循环方法来判断
forkeyindict.keys():
但是这种方法毕竟不够简洁,
查看字典所有键值内容
>>>dict={'ob1':
'computer','ob2':
'mouse','ob3':
'printer'}
>>>dict.values()
['mouse','printer','computer']
列出所有项目
>>>dict.items()
[('ob2','mouse'),('ob3','printer'),('ob1','computer')]
清空字典
>>>dict.clear()
>>>dict
{}
拷贝字典
>>>dict
{'ob2':
'mouse','ob3':
'printer','ob1':
'computer'}
>>>a=dict
>>>a
{'ob2':
'mouse','ob3':
'printer','ob1':
'computer'}
>>>b=dict.copy()
>>>b
{'ob2':
'mouse','ob3':
'printer','ob1':
'computer'}
比较字典
>>>cmp(a,b)
首先比较主键长度,然后比较键大小,然后比较键值大小,(第一个大返回1,小返回-1,一样返回0)
更新字典
>>>dict={'yangrong':
{"age":
"23","job":
"IT"},"xiaohei":
{"'age':
'22','job':
'dota'"}}
>>>dict
{'xiaohei':
set(["'age':
'22','job':
'dota'"]),'yangrong':
{'age':
'23','job':
'IT'}}
>>>dict['xiaohei']=111 #修改一级字典
>>>dict
{'xiaohei':
111,'yangrong':
{'age':
'23','job':
'IT'}}
>>>dict['yangrong']['age']=25 #修改二级字典
>>>dict
{'xiaohei':
111,'yangrong':
{'age':
25,'job':
'IT'}}
>>>dict={'yangrong':
['23','IT'],"xiaohei":
['22','dota']}
>>>dict['xiaohei'][1]="dota2" #修改字典中列表某项,1是代表列表中第2个字符串。
>>>dict
{'xiaohei':
['22','dota2'],'yangrong':
['23','IT']}
删除字典元素
>>>dict
{'xiaohei':
['22','dota2'],'yangrong':
['23','IT']}
>>>deldict['xiaohei'] #删除xiaohei键值
>>>dict
{'yangrong':
['23','IT']}
>>>dict
{'yangrong':
['23','IT']}
>>>
>>>deldict['yangrong'][1] #删除yangrong主键的每2字值
>>>dict
{'yangrong':
['23']}
删除整个字典
>>>dict
{'yangrong':
['23']}
>>>dict.clear() #同deldict
>>>dict
{}
将字符串切分为列表
>>>s="helloworldbye"
>>>s.split() #用于把有规律的文本,读取出来后,使用列表进行修改,再写入文件。
['hello','world','bye']
将列表转换为字符串
S.split(str,'') #将string转list,以空格切分
存储字典(pickle序列化)
#需导入pickle模块 importpickle
把字典内容存入文件
f=file('data.txt','wb') #新建文件data.txt,'wb',b是打开块文件,对于设备文件有用
pickle.dump(a,f) #把a序列化存入文件
f.close()
把内容读入字典(反序列化)
a=open('data.txt','rb') #打开文件
printpickle.load(a) #把内容全部反序列化
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 字典相关操作 字典 相关 操作