python3_字典
来源:互联网 发布:个人淘宝信誉怎么提升 编辑:程序博客网 时间:2024/04/30 03:34
# -*- coding: utf-8 -*-class ShowDict(object): '''该类用于展示字典的使用方法 ''' def __init__(self): self.spiderMan = self.createDict() self.insertDict(self.spiderMan) self.modifyDict(self.spiderMan) self.operationDict(self.spiderMan) self.deleteDict(self.spiderMan) def createDict(self): print(u"创建字典:") print(u"执行命令spiderMan = {'name':'Peter Parker','sex':'male','Nation':'Americ','college':'MIT'}") spiderMan = {'name': 'Peter Parker', 'sex': 'male', 'Nation': 'Americ', 'college': 'MIT'} self.showDict(spiderMan) return spiderMan def showDict(self, spiderMan): print(u"显示字典") print(u"spiderMan = "), print(spiderMan) print('\n') def insertDict(self, spiderMan): print(u"字典中添加键age,值为31") print(u"执行命令spiderMan['age'] = 31") spiderMan['age'] = 31 self.showDict(spiderMan) def modifyDict(self, spiderMan): print(u"字典修改键'college'的值为'Empire State University'") print(u"执行命令spiderMan['college'] = 'Empire State University'") spiderMan['college'] = 'Empire State University' self.showDict(spiderMan) def operationDict(self, spiderMan): print(u"字典的其它操作方法") print(u"###########################") print(u"显示字典所有的键,keyList = spiderMan.keys()") keyList = spiderMan.keys() print(u"keyList = "), print(keyList) print('\n') print(u"显示字典所有键的值,valueList = spiderMan.values()") valueList = spiderMan.values() print(u"valueList = "), print(valueList) print('\n') print(u"显示字典所有键和值的元组,itemList = spiderMan.items()") itemList = spiderMan.items() print(u"itemList = "), print(itemList) print('\n') print(u"取字典中键为college的值,college = spiderman.get('college')") college = spiderMan.get('college') print(u"college = %s" % college) print('\n') def deleteDict(self, spiderMan): print(u"删除字典中键为Nation的值") print(u"执行命令 del(spiderMan['Nation'])") del (self.spiderMan['Nation']) self.showDict(spiderMan) print(u"清空字典中所有的值") print(u"执行命令 spiderMan.clear()") self.spiderMan.clear() self.showDict(spiderMan) print(u"删除字典") print(u"执行命令 del(spiderMan)") del (spiderMan) print(u"显示spiderMan") try: self.showDict(spiderMan) except NameError: print(u"spiderMan 未被定义")if __name__ == '__main__': sd = ShowDict()
阅读全文
0 0
- python3_字典
- python3_字符串
- python3_列表
- python3_元组
- Python3_模拟登录
- Python3_三级菜单
- Python3_购物车
- Python3_工资管理系统
- python3_ 廖雪峰 邮件发送代码测试
- python3_访问url、json、读写文件
- python3_检查URL是否能正常访问
- 【DayDayUp】Python3_爬虫_入门前准备(吐槽)
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- U-Boot源代码阅读笔记(二) —— 对lowlevel_init.S的分析
- [codevs]: 线段树练习1-4
- Mybatis5.0.2分页插件
- bmob和okgo的冲突
- javaoop总结
- python3_字典
- hibernate-关系映射(一对一(单向、双向))-8
- EasyDarwin开源流媒体服务器中一种实现对作用域内new对象自动释放的方法(值得借鉴)
- lowlevel_init.S 分析
- IMWeb提升营Day5 | 训练题27:字符串的排列
- L1-009. N个数求和
- MSDN例子 获取网卡、ip等信息
- Android adb命令介绍
- MySQLTuner--使用方法