Python之路 day2 初识字典
#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 初识字典的更多相关文章
- Python之路 day2 字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...
- 小白的Python之路 day2 字符串操作 , 字典操作
1. 字符串操作 特性:不可修改 name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '- ...
- Python之路 day2 字典练习题之 三级菜单
#Author:ersa ''' 程序: 三级菜单 要求: 打印省.市.县三级菜单 可返回上一级 可随时退出程序 ''' menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{ ...
- Python之路,Day2 - Python基础(转载Alex)
Day2-转自金角大王 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存 ...
- Python之路,Day2 - Python基础2
def decode(self, encoding=None, errors=None): """ 解码 """ ""& ...
- Python之路Day2
-->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...
- Python之路 day2 字符编码及转换
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa import sys print("sys default encodin ...
- Python之路 day2 文件基础操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open( ...
- Python之路 day2 集合的基本操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #集合是无序的 集合的关系测试, 增加,删除,查找等操作 ''' #列表去重 ...
随机推荐
- Java并发控制:ReentrantLock Condition使用详解
生产者-消费者(producer-consumer)问题,也称作有界缓冲区(bounded-buffer)问题,两个进程共享一个公共的固定大小的缓冲区.其中一个是生产者,用于将消息放入缓冲区:另外一个 ...
- [SLAM]2D激光线特征提取
Nguyen, V., et al. (2007)."A comparison of line extraction algorithms using 2D range data for i ...
- Debian系列Linux/Ubuntu 安装软件
wps(http://community.wps.cn/download/) 优客天气(https://launchpad.net/indicator-china-weather/+download) ...
- ajax 使用
写在前面的话: 用了很久的Asp.Net Ajax,也看了段时间的jquery中ajax的应用,但到头来,居然想不起xmlHttpRequest的该如何使用了. 以前记的也不怎么清楚,这次就重新完整的 ...
- ASP.NET MVC 3 CheckBoxList 的使用
在以前的 ASP.NET MVC 中可以直接使用 CheckBoxList,但后来不知道什么原因在 MVC 中移除了 CheckBoxList,所以默认情况下 ASP.NET MVC 3 中是没有 C ...
- AJAX-----11iframe模拟ajax文件上传效果原理3
如果直接给用户提示上传成功,那么如果用户上传的文件比较大点,那么等上半天都没反映,那么用户很有可能会刷新或者关了从来等... 那么会给我们服务器带来一定的影响,所以我们可以对这方面的用户体验度进行提升 ...
- 循环多次ajax请求
最近在做网页前端,其中有个功能按钮是从数据表格中同时删除多条数据,涉及到循环多次ajax请求 但是老是出现一些请求被忽视的情况,应该是由于for循环在极短时间内被完成,所以第一次请求后的几次请求时,x ...
- Java 泛型和通配符解惑
转自:http://www.linuxidc.com/Linux/2013-10/90928.htm T 有类型 ? 未知类型 一.通配符的上界 既然知道List<Cat>并不是Lis ...
- sprint2 项目的粗略展示
- 关于一个新的DOM选择器querySelector
在传统的javascript中,提到DOM选择器,大家比较熟悉的方式是通过tag,name,id来获取,其实大家都发现如果获取比较复杂的话,用这个方法会很繁琐,这时大家应该都会想到jquery里获取一 ...