python3字典详解_python3中字典详解
字典(dict)
1. 创建字典的几种方式
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
使用上面的方法构建字典`{"one:1", "two":2, "three":3}
方法1构建字典`:
a = dict(one=1, two=2, three=3)
a
输出结果:
{'one': 1, 'three': 3, 'two': 2}
方法2构建字典:
a = dict({'one': 1, 'two': 2, 'three': 3})
a
输出结果:
{'one': 1, 'three': 3, 'two': 2}
方法3构建字典:
d = dict([('two', 2), ('one', 1), ('three', 3)])
print('d =', d)
e = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
print('e =', e)
输出结果:
d = {'one': 1, 'three': 3, 'two': 2}
e = {'one': 1, 'three': 3, 'two': 2}
方法4构建字典:
d = {'one': 1, 'two': 2, 'three': 3}
d
输出结果:
{'one': 1, 'three': 3, 'two': 2}
方法5构建字典:
# 创建一个空字典
a = dict()
# 通过赋值语句构造字典
a['one'] = 1
a['two'] = 2
a['three'] = 3
a
输出结果:
{'one': 1, 'three': 3, 'two': 2}
2. 对字典可以运用的一些操作
len(d): 返回字典的长度
a = dict(one=1, two=2, three=3)
len(a)
输出结果:
3
d[key1]:返回字典中key等于key1的值
a = dict(one=1, two=2, three=3)
a['one']
输出结果:
1
del d[key]:删除字典中key为key的值
a = dict(one=1, two=2, three=3)
del a['two']
a
输出结果:
{'one': 1, 'three': 3}
key in d 或者 key not in d:判断字典中有没有key为key的值
a = dict(one=1, two=2, three=3)
print('one' in a)
print('four' in a)
输出结果:
True
False
iter(d):返回字典中key的迭代器
a = dict(one=1, two=2, three=3)
d_iter = iter(a)
[x for x in d_iter]
输出结果:
['one', 'three', 'two']
3. 字典中的方法
dic.clear():将字典清空
a = dict(one=1, two=2, three=3)
a.clear()
print(a)
输出结果:
{}
dic.copy():浅复制复制一个字典。浅拷贝不会拷贝子对象,所以原始数据改变,子对象也会改变
a = dict(one=1, two=2, three=3)
b = a.copy()
print('a =', a)
print('b =', b)
# 更改b中的值
b['four'] = 4
print('updated:')
print('a =', a)
print('b =', b)
# 另外一种情况
print()
x = {"a": 123, "b": 444, "c": [1, 2, 6, "asd"]}
y = x.copy()
y["c"].remove("asd")
y["a"] = "xxx"
print('x=', x)
print('y=', y)
输出结果:
a = {'one': 1, 'three': 3, 'two': 2}
b = {'one': 1, 'three': 3, 'two': 2}
updated:
a = {'one': 1, 'three': 3, 'two': 2}
b = {'one': 1, 'three': 3, 'two': 2, 'four': 4}
x= {'a': 123, 'c': [1, 2, 6], 'b': 444}
y= {'a': 'xxx', 'c': [1, 2, 6], 'b': 444}
dic.items():返回字典中(key,value)对的迭代器
a = dict(one=1, two=2, three=3)
print(a.items())
输出结果:
dict_items([('one', 1), ('three', 3), ('two', 2)])
dic.keys():返回字典中key的迭代器
a = dict(one=1, two=2, three=3)
print(a.keys())
输出结果:
dict_keys(['one', 'three', 'two'])
dic.values():返回字典中值的迭代器
a = dict(one=1, two=2, three=3)
print(a.values())
输出结果:
dict_values([1, 3, 2])
dic.update([other]):更新字典
a = dict(one=1, two=2, three=3)
# 使用参数
a.update(four=4)
print(a)
# 使用字典来更新
other = {'five': 5, 'six': 6}
a.update(other)
print(a)
# 使用迭代器
a.update([('seven', 7),('eight', 8)])
print(a)
# 注意上面使用字典和迭代器来更新字典时,需要增加的字典和迭代器的长度大于2,否则会出现错误
输出结果:
{'one': 1, 'three': 3, 'two': 2, 'four': 4}
{'one': 1, 'three': 3, 'six': 6, 'five': 5, 'two': 2, 'four': 4}
{'one': 1, 'seven': 7, 'three': 3, 'eight': 8, 'six': 6, 'five': 5, 'two': 2, 'four': 4}
dic.popitem():随机删除一项,并返回键值对
a = dict(one=1, two=2, three=3)
print(a.popitem())
print('a =', a)
输出结果:
('one', 1)
a = {'three': 3, 'two': 2}
dic.pop(key[,default):删除并返回给定键的值,并删除键值对
a = dict(one=1, two=2, three=3)
print(a.pop('one'))
print(a.pop('four', 4))
输出结果:
1
4
dic.get(key[,default]):返回给定key的值,如果字典中没有key,则返回default值
a = dict(one=1, two=2, three=3)
print(a.get('two'))
print(a.get('four'), 3)
输出结果:
2
None 3
dic.setdefault(key[,default]):和dic.get()类似,如果没有key,则将{key:default}添加到字典中
a = dict(one=1, two=2, three=3)
print(a.setdefault('two'))
print(a.setdefault('four', 4))
print(a)
输出结果:
2
4
{'one': 1, 'three': 3, 'two': 2, 'four': 4}
python3字典详解_python3中字典详解相关推荐
- python编程字典100例_python中字典(Dictionary)用法实例详解
本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的"键-值对"组成. ...
- [转载] python里字典的用法_python中字典(Dictionary)用法实例详解
参考链接: Python字典dictionary copy方法 本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映 ...
- python3.4新特性_Python3中的新特性(1)——新的语言特性
1.源代码编码和标识符 Python3假定源代码使用UTF-8编码.另外,关于标识符中哪些字符是合法的规则也放宽了.特别是,标识符可以包含代码点为U+0080及以上的任意有效Unicode字符.例如: ...
- python3版本代码大全_python3中的
出品 | FlyAI 编译 | 林椿眄 编辑 | Donna Python 已经成为机器学习及其他科学领域中的主流语言.它不但与多种深度学习框架兼容,而且还包含优秀的工具包和依赖库,方便我们对数据进行 ...
- python列表按照指定顺序排序-python列表排序、字典排序、列表中字典排序
手记 -- encoding=utf-8 -- python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} sor ...
- python 去重 字典_python按照list中字典的某key去重的示例代码
一.需求说明 当我们写爬虫的时候,经常会遇到json格式的数据,它通常是如下结构: data = [{'name':'小K','score':100}, {'name':'小J','score':98 ...
- python3字典详解_Python3中Dictionary(字典)操作详解
在绝大部分的开发语言中与实际开发过程中,Dictionary扮演着举足轻重的角色.从我们的数据模型到服务器返回的参数到数据库的应用等等,Dictionary的身影无处不在.那么,在Python中,Di ...
- python中def的用法详解_Python3中def的用法
python中的def关键字是用来定义函数的. 云海天教程网,大量的免费python教程,欢迎在线学习! 定义函数,也就是创建一个函数,可以理解为创建一个具有某些用途的工具.定义函数需要用 def 关 ...
- 详解Python中的序列解包(2)
8个月前曾经发过一篇关于序列解包的文章,见详解Python序列解包,本文再稍作补充. 可以说,序列解包的本质就是把一个序列或可迭代对象中的元素同时赋值给多个变量,如果等号右侧含有表达式,会把所有表达式 ...
最新文章
- 删除ctrl alt del更改密码
- List 去除重复数据的五种方式,舒服~
- win服务器管理器“丢失”了怎么办?
- go hive skynet_云风的skynet在国内外来看究竟算什么水平?可以一统国内游戏服务端框架吗?...
- 《Effective Debugging:软件和系统调试的66个有效方法》一第5条:在能够正常运作的系统与发生故障的系统之间寻找差别...
- java获取数据库连接语句_JAVA连接数据库语句
- 学习Spring Boot:(九)统一异常处理
- C++设计模式-Facade模式
- python内嵌函数和闭包与java 匿名内部类_Java匿名内部类构造原理分析
- 01-NLP-02-gensim中文处理案例
- composer全局 linux_Linux下全局安装composer方法
- 操作系统——零碎概念
- 物联网单片机毕业设计实现
- Unity SRP自定义渲染管线学习1.1:初步搭建
- 基于javaweb的户籍管理系统
- Excel操作:分析工具库
- 移动硬盘无法访问需要格式化,怎样恢复移动硬盘数据
- 面试题:你印象最深刻的两个bug是什么,你是怎么解决的?
- [kubernetes]-kubernetes+nfs创建高可用mysql
- Xcode8快速注释插件无法使用