python3学习笔记七(字典)
参照http://www.runoob.com/python3/python3-dictionary.html
字典
字典是另一种可变容器模型,且可以存储任意类型对象。
dict1 = {key1:value1,key2:value2}
注意:键key是唯一的,但是值不用,值可以是字符串、数字或元组。
dict2 = {'a':1,'b':'12'}
dict3 = {'Name':'jack','Age':'22','Class':'first'}
print("dict3['Name']: ",dict3['Name']) #访问字典中的值
print("dict3['Age']: ",dict3['Age'])
dict3['Age'] = 20
dict3['School'] = 'python' #修改字典
print("dict3['Age']: ",dict3['Age'] )
print("dict3['School'] ",dict3['School'])
del dict3['Name'] #删除字典中的元素
print(dict3)
dict3.clear()
# del dict3 删除字典
print(dict3)
字典值的特性:
不允许一个键出现两次;
键必须不可变,可以用数字、字符串或元组,但是列表不行。
字典内置函数&方法

内置方法如下:
#radiansdict.clear() 删除字典内所有元素
dict4 = {'name':'bob','age':19}
print(dict4)
dict4.clear()
print(dict4)
print(len(dict4))
#radiansdict.copy() 返回一个字典的浅复制
dict5 = {'name':'bob','age':19,'class':'two'}
dict6 = dict5.copy()
print('新复制的字典是: ',dict6)
'''
#radiansdict.fromkeys()
# 创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
seq -- 字典键值列表。
value -- 可选参数, 设置键序列(seq)的值。
'''
dict5 = {'name':'bob','age':19,'class':'two'}
dict7 = dict5.fromkeys(dict5)
print('新的字典是: %s' % str(dict7))
dict8 = dict5.fromkeys(dict5,10)
print('新的字典是: %s' % str(dict8)) # radiansdict.get(key, default=None) 返回指定键的值,如果值不在字典中返回default值
dict5 = {'name':'bob','age':19,'class':'two'}
print('age的值是: %s' % dict5.get('age'))
print('sex的值是: %s' % dict5.get('sex','na'))
# key in dict 如果键在字典dict里返回true,否则返回false
dict5 = {'name':'bob','age':19,'class':'two'}
if 'age' in dict5:
print('age键存在')
else :
print('age键不存在')
if 'sex' in dict5:
print('sex键存在')
else :
print('sex键不存在')
# not in 同理可知
# radiansdict.items() 以列表返回可遍历的(键, 值) 元组数组
print("value : %s" % dict5.items())
# radiansdict.keys() 返回一个迭代器,可以使用 list() 来转换为列表
dict5.keys()
print(list(dict5.keys()))
'''
radiansdict.setdefault(key, default=None)
和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default
key -- 查找的键值。
default -- 键不存在时,设置的默认键值。
'''
print("age键的值是 :%s" % dict5.setdefault('age',None))
print("sex键的值是 : %s" % dict5.setdefault('sex',None))
print("新字典是: ", dict5)
# radiansdict.update(dict2) 把字典dict2的键/值对更新到dict里
dict5 = {'name':'bob','age':19,'class':'two'}
dict7 = {'sex':'male'}
dict5.update(dict7)
print("更新字典dict5: ", dict5) # radiansdict.values() 返回一个迭代器,可以使用 list() 来转换为列表
print("字典所有值是: ", list(dict5.values())) # pop(key[,default]) 删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
dict5 = {'name':'bob','age':19,'class':'two'}
pop_obj = dict5.pop('name')
print(pop_obj) # popitem() 随机返回并删除字典中的一对键和值(一般删除末尾对)。
dict5 = {'name':'bob','age':19,'class':'two'}
pop_obj = dict5.popitem()
print(pop_obj)
print(dict5)
python3学习笔记七(字典)的更多相关文章
- python学习笔记七——字典
4.3 字典结构 字典是Python中重要的数据类型,字典的由“键-值”对组成的集合,字典中的“值”通过“键”来引用. 4.3.1 字典的创建 字典由一系列的“键-值”(key-value)对组成,“ ...
- python3.4学习笔记(七) 学习网站博客推荐
python3.4学习笔记(七) 学习网站博客推荐 深入 Python 3http://sebug.net/paper/books/dive-into-python3/<深入 Python 3& ...
- Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html
Python3学习笔记(urllib模块的使用) 1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- Python3学习笔记 - 准备环境
前言 最近乘着项目不忙想赶一波时髦学习一下Python3.由于正好学习了Docker,并深深迷上了Docker,所以必须趁热打铁的用它来创建我们的Python3的开发测试环境.Python3的中文教程 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- python3学习笔记(7)_listComprehensions-列表生成式
#python3 学习笔记17/07/11 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #通过列表生成式可以生成格式各样的list,这种list 一 ...
- python3学习笔记(6)_iteration
#python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #类似 其他语言的for循环,但是比for抽象程度更高 # f ...
随机推荐
- 树状DP HDU1520 Anniversary party
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...
- 从Excel、CSV文件获取数据
#region 从Excel获取数据 /// <summary> /// 从Excel获取数据 /// </summary> /// <param name=" ...
- 分布式监控系统(类zabbix)
目录: 为什么要做监控? 监控系统业务需求分析: 监控系统架构设计: 监控系统表结构设计: 一.为什么要做监控系统? 市面上已经有很多成熟的监控系统,例如zabbix.nagios,为什么自己开发监控 ...
- kafka producer 发送消息简介
kafka 的 topic 由 partition 组成,producer 会根据 key,选择一个 partition 发送消息,而 partition 有多个副本,副本有 leader 和 fol ...
- Eclipse+Spring boot开发教程
一.安装 其实spring boot官方已经提供了用于开发spring boot的定制版eclipse(STS,Spring Tool Suite)直接下载使用即可,但考虑到可能有些小伙伴不想又多装个 ...
- anguar6中 无法在Element上找到属性 (eg 原DOM的offsetTop)
let aa=this.elementRef.nativeElement.querySelector('.logBox') ;
- boost range zhuan
Officialhttp://67.223.234.84/boost_doc/libs/range/doc/utility_class.html#sub_range http://blog.sina. ...
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers 一.前言 上一篇博客< ...
- 高性能场景下,HashMap的优化使用建议
1. HashMap 在JDK 7 与 JDK8 下的差别 顺便理一下HashMap.get(Object key)的几个关键步骤,作为后面讨论的基础. 1.1 获取key的HashCode并二次加工 ...
- python3入门教程(一)之 hello world
概述 python 这门语言这几年非常的火,很多技术都用的到,像爬虫,大数据,人工智能等,包括很多的小孩都首选python作为入门学习语言,那python 究竟是怎样一门语言呢? Python 是一个 ...