#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. elasticsearch使用操作部分

    本片文章记录了elasticsearch概念.特点.集群.插件.API使用方法. 1.elasticsearch的概念及特点.概念:elasticsearch是一个基于lucene的搜索服务器.luc ...

  2. iOS一些系统事件的生命周期

    1.- (void)applicationWillResignActive:(UIApplication *)application 说明:当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息 ...

  3. mongodb配置

    Mongodb1. 安装2. CRUD3. 索引4. 副本及(replica sets)5. 分片(sharding) nosql 简单数据模型 元数据和应用数据分离 弱一致性 优势: 避免不必要的复 ...

  4. Unity基于响应式编程(Reactive programming)入门

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  5. MVC中的JS和CSS压缩

    小说一下Js和CSS压缩的好处: 1.减小了文件的体积 2.减小了网络传输量和带宽占用 3.减小了服务器的处理的压力 4.提高了页面的渲染显示的速度  很多建议将站点的静态文件(如图片.js.css ...

  6. 各大Oj平台介绍 刷题平台

    https://leetcode.com/ http://www.cnblogs.com/lzmfywz/archive/2012/02/07/2342010.html 1.题库与网站资源题库-在线提 ...

  7. 让hadoop-0.20.2自带的eclipse插件支持eclipse-3.5以上

    hadoop-0.20.2自带的eclipse插件是不支持eclipse-3.5以上的,要想让它支持3.5以上就必须重新编译eclipse插件. 首先先修改  hadoop-0.20.2\src\co ...

  8. dr.wondr博士随笔之三星某古董智能机GTXXXX 的取证恢复一例

    大家好!欢迎来到我dr.wonde博士的微博! 这是dr.wonde的第一篇微博,不足之处,还请见谅. 今天dr.wonde给你们带来不可能的数据恢复任务之三星非智能机古董机GT-E1088C 的恢复 ...

  9. 谈谈JDK线程的伪唤醒

    在JDK的官方的wait()方法的注释中明确表示线程可能被"虚假唤醒",JDK也明确推荐使用while来判断状态信息.那么这种情况的发生的可能性有多大呢? 使用生产者消费者模型来说 ...

  10. websphere如何删除应用程序服务器(概要管理工具)

    在IBM WebSphere 的概要管理工具中我们可以新建一个应用程序服务器,但是工具中并未提供删除已经建过的应用程序服务器.下面 交大家一个比较简单的方法来删除应用程序服务器 图片中可以看到,我已经 ...