#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. wpa supplicant 保存 wifi 设置

    wpa suppliclant使用wpa gui连接wifi后,下次开机的时,不能保存,需要从新手动进行连接. 自动保存方法: 配置文件/etc/wpa_supplicant.conf 添加 upda ...

  2. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

  3. zjuoj 3607 Lazier Salesgirl

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607 Lazier Salesgirl Time Limit: 2 Sec ...

  4. IOS基础面试题

    最近离职了,找工作,光会做项目,对基础不熟,今天就总结了一点面试题. 废话不多说,上题吧: 1.objective-c中的数字对象都有哪些,简述它们与基本数据类型的区别是什么. 基本类型和C一样,主要 ...

  5. sql server 中一次insert 多条的写法

    1.SELECT INTO FROM语句 注意此处 要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中.示例如下 2.INSERT ...

  6. Struts2 配置详解

    1. web.xml 此文件的配置可以参看struts2的示例文档 <filter> <filter-name>struts2</filter-name> < ...

  7. dump_stack调用过程【原创】

    dump_stack调用: void dump_stack(void){    dump_backtrace(NULL, NULL);} 继续看: static void dump_backtrace ...

  8. maven 编译插件

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compi ...

  9. LA 3713 宇航员分组

    题目链接:http://vjudge.net/contest/142615#problem/B 题意:有A,B,C三个人物要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于 ...

  10. 2016年江西理工大学C语言程序设计竞赛(初级组)

    问题 A: 木棒根数 解法:把所有的情况保存下来,加一下就好 #include<bits/stdc++.h> using namespace std; map<char,int> ...