#Author:ersa
'''
key-value 键值对
字典是无序的,不需要下标,有key
字典的查找、修改、添加、判断、删除
''' info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
} #输出
print("输出所有的key".center(50,'='))
print(info.keys())
print("=".center(70,'=')) print("输出所有的 值".center(50,'='))
print(info.values())
print("=".center(70,'=')) #循环输出键-值
print("输出键-值".center(50,'='))
for i in info:
print(i,info[i])
print("=".center(70,'=')) #设置默认值:如果有key-value 则返回,没有则创建
print("取key-value值,没有则创建".center(50,'='))
info.setdefault("stu1106","Alex")
print(info)
print("=".center(70,'=')) #查找
print(info)
print("查找".center(50,'='))
print(info["stu1101"]) #安全查找--->查找的值不存在时不会报错
print("安全查找".center(50,'='))
print(info.get('stu1104')) #判断
print("判断key是否存在".center(50,'='))
print('stu1104' in info) #修改
print("修改".center(50,'='))
print(info) info["stu1101"] = "武藤兰"
#添加
print("添加".center(50,'='))
info["stu1104"] = "苍老师"
print(info["stu1104"]) #删除 Python--->通用删除del
print("删除".center(50,'='))
#del info["stu1101"]
print(info) #字典删除
print("字典删除".center(50,'='))
#info.pop("stu1101")
print(info) #随机删除
print("随机删除".center(50,'='))
#info.popitem()
print(info) #字典转列表
print("字典转列表".center(50,'='))
print(info.items())
print("=".center(70,'=')) #update 更新字典
b = {
'stu1101':"alex",
1:3,
2:5
}
info.update(b)
print("合并字典,有重合key,则更新".center(50,'='))
print(info)
print("=".center(70,'=')) #初始化新的字典
print("初始化新的字典".center(50,'='))
c = dict.fromkeys([6,7,8],'test')
print(c)
print("=".center(70,'='))

Python之路 day2 初识字典的更多相关文章

  1. Python之路 day2 字符串/元组/列表/字典互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  2. 小白的Python之路 day2 字符串操作 , 字典操作

    1. 字符串操作 特性:不可修改 name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '- ...

  3. Python之路 day2 字典练习题之 三级菜单

    #Author:ersa ''' 程序: 三级菜单 要求: 打印省.市.县三级菜单 可返回上一级 可随时退出程序 ''' menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{ ...

  4. Python之路,Day2 - Python基础(转载Alex)

    Day2-转自金角大王 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存 ...

  5. Python之路,Day2 - Python基础2

    def decode(self, encoding=None, errors=None): """ 解码 """ ""& ...

  6. Python之路Day2

    -->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...

  7. Python之路 day2 字符编码及转换

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa import sys print("sys default encodin ...

  8. Python之路 day2 文件基础操作

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open( ...

  9. Python之路 day2 集合的基本操作

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #集合是无序的 集合的关系测试, 增加,删除,查找等操作 ''' #列表去重 ...

随机推荐

  1. JS之访问器

    1.在对象中定义get,set访问器属性 <script> var test = { _name:"pmx", _age:18, _born:1990, get nam ...

  2. MyBatis操作指南-配置结果映射一对一,一对多,多对多(基于注解)

  3. jQuery源代码阅读之二——jQuery静态属性和方法

    一.jQuery.extend/jQuery.fn.extend //可接受的参数类型如下:jQuery.extend([deep],target,object1,[objectN]) jQuery. ...

  4. Java 使用jaxp修改节点

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <perso ...

  5. Asp.net导出Excel乱码的解决方法

    通过跟踪Asp.net服务器代码,没有乱码,然而导出Excel到浏览器后,打开时出现乱码. 解决方法是添加编码格式的前缀字节码:Response.BinaryWrite(System.Text.Enc ...

  6. Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies

    安装对应的 Microsoft Visual C++ 2010 Redistributable Package (x86)   If your download does not start afte ...

  7. 将n*n矩阵顺时针旋转90度

    /** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...

  8. spring aop 中获取 request

    使用aop时需要request 和response 使用方法调用时 HttpServletRequest request = ((ServletRequestAttributes)RequestCon ...

  9. Qt报表控件NCReport教程:添加报表字段

    NCReport是一款轻量级.快速.多平台.简单易用的基于Qt toolkit的C++编写的报表解决方案,目前主要包括报表渲染库和报表设计器GUI应用程序. 在前面的文章<报表控件NCRepor ...

  10. xcode编译错误

    1.xcode无效文件的编译错误. 问题: clang: error: no such file or directory: '/Users/admin/client/trunk/sengoku_sc ...