Python Sting 练习实践(二)
2017-08-19 16:13
169 查看
字典增删改查
Student_Info = { '1220610001':'Alex', '1220610002':'Bob', '1220610003':'Cindy', '1220610004':'David'} print("info:",Student_Info) print("pop('1220610004'):",Student_Info.pop('1220610004'))#del print(Student_Info) print("find 1220610001 :",Student_Info["1220610001"])#search1 print("1220610004 is in info?-->",Student_Info.get('1220610004'))#search2 print("1220610003 is in info?-->",Student_Info.get('1220610003'))#search2 print("1220610004 is in info?-->",'1220610004' in Student_Info)#search3 print("1220610003 is in info?-->",'1220610003' in Student_Info)#search3 Student_Info['1220610005']='Eric' print(Student_Info)#add Student_Info['1220610005']='Ellen' print(Student_Info)#change
多级字典嵌套
Student_Info = { '1220610001':{ 'Alex':{ 'English':[90, 91, 93]}}, '1220610002':{ 'Bob':{ 'English':[94, 95, 96]}}, '1220610003':{ 'Cindy':{ 'English':[97, 98, < 4000 span class="hljs-number">99]}} } Student_Info['1220610003']['Cindy']['English'][0]=60 print(Student_Info) print(Student_Info.values()) print(Student_Info.keys()) Student_Info.setdefault("1220610003",{'Cindy':{'English':[10,20,30]}})#If exist, not changed print(Student_Info) Student_Info.setdefault("1220610004",{'David':{'English':[10,20,30]}})#If not exist, added print(Student_Info)
字典交叉合并
Student_Info = { '1220610001':{ 'Alex':{ 'English':[90, 91, 93]}}, '1220610002':{ 'Bob':{ 'English':[94, 95, 96]}}, '1220610003':{ 'Cindy':{ 'English':[97, 98, 99]}} } New_Student = { '1220610001':{ 'Angela':{ 'English':[90, 91, 93]}}, '1220610004':{ 'Daniel':{ 'English':[94, 95, 96]}}, } print(Student_Info) Student_Info.update(New_Student) print(Student_Info)
Change to list (items)
Student_Info = { '1220610001':{ 'Alex':{ 'English':[90, 91, 93]}}, '1220610002':{ 'Bob':{ 'English':[94, 95, 96]}}, '1220610003':{ 'Cindy':{ 'English':[97, 98, 99]}} } info = {"Name":'Alex',"Age":25} print(Student_Info.items()) print(info.items())
字典的循环
Student_Info = { '1220610001':{ 'Alex':{ 'English':[90, 91, 93]}}, '1220610002':{ 'Bob':{ 'English':[94, 95, 96]}}, '1220610003':{ 'Cindy':{ 'English':[97, 98, 99]}} } for i in Student_Info: print(i,Student_Info[i])#more efficient print() for k,v in Student_Info.items(): print(k,v)
相关文章推荐
- Python Sting 练习实践(一)
- Python List 练习实践(二)
- python从入门到实践第三章的课后练习作业
- python编程:从入门到实践-第十章练习
- 《python机器学习及实践》书籍代码练习
- Python小练习--类(来自《Python编程从入门到实践》)
- Python List 练习实践(一)
- python专题练习
- python的练习方法
- Python实例练习3
- Python实践-itchat获取微信好友总人数以及男女比例
- 输出n的因子 分类: python 小练习 2013-12-13 17:20 229人阅读 评论(0) 收藏
- python-练习3
- 利用 Python 练习数据挖掘
- python多线程练习
- 机器学习算法与Python实践之(六)二分k均值聚类
- Python练习实例13
- python 练习
- python中turtle模块的练习
- Python机器学习及实践——基础篇5(K近邻)