Python3字典笔记

简介: Python字典笔记,学习过程中的记录

字典类型:

  • 散列类型
  • 键值对的形式 key:value (可变类型)

例子:

dict= {'name':'夏柔','age':'18'}
wpon=dict{name='夏柔', age='18'}

通过key 来获取值 key:value

例子:

>>>dict{'name': '夏柔', 'age': '18'}
>>>dict['name']
'夏柔'

通过key来替换值

例子:

>>>dict{'name': '夏柔', 'age': '18'}
>>>dict['name'] = ['小夏柔']
>>>dict{'name': ['小夏柔'], 'age': '18'}
>>>dict['name']
['小夏柔']

通过key来添加值

例子:

>>>dict{'name': ['小夏柔'], 'age': '18'}
>>>dict['sex']='boy'>>>dict{'name': ['小夏柔'], 'age': '18', 'sex': 'boy'}
>>>dict['sex']
'boy'# 有sex 的值则替换

增加方法

.fromkeys() 返回一个新的字典: (key)

dict= {'name':123,'age':18,'hight':189}
wpon=dict.fromkeys(['名字','年龄','身高'])
print(wpon)
输出结果:
{'名字': None, '年龄': None, '身高': None}

注意, None意思为空的

.fromkeys() 返回一个新的字典: (value)

dict= {'name':123,'age':18,'hight':189}
wpon=dict.fromkeys(['名字','年龄','身高'],'夏柔')
print(wpon)
输出结果:
{'名字': '夏柔', '年龄': '夏柔', '身高': '夏柔'}

扩展: 如果有一样的key值, 则覆盖掉前面的值(value)

有则查, 无则增 .setdefault()

dict= {'name':123,'age':18,'hight':189}
# wpon = dict.fromkeys(['名字','年龄','身高'],'夏柔')# dict.setdefault('name')print(dict.setdefault('name'))
输出结果:
123

删除方法 .pop()

dict= {'name':123,'age':18,'hight':189}
# wpon = dict.fromkeys(['名字','年龄','身高'],'夏柔')dict.pop('name')
print(dict)
输出结果:
{'age': 18, 'hight': 189}

随机删除 .popitem()

dict= {'name':123,'age':18,'hight':189}
# wpon = dict.fromkeys(['名字','年龄','身高'],'夏柔')dict.popitem()
print(dict)
输出结果:
{'name': 123, 'age': 18}

全部删除 .clear()

dict= {'name':123,'age':18,'hight':189}
# wpon = dict.fromkeys(['名字','年龄','身高'],'夏柔')dict.clear()
print(dict)
输出结果:
{}

更新 .update()

dict= {'name':123,'age':18,'hight':189}
dict.update({'name':666,'age':18,'hight':189})
print(dict)
输出结果:
{'name': 666, 'age': 18, 'hight': 189}

传入:

dict= {'name':123,'age':18,'hight':189}
wpon= {'sex':'boy'}
dict.update(wpon)
print(dict)
输出结果:
{'name': 123, 'age': 18, 'hight': 189, 'sex': 'boy'}

查 .get(key)

dict= {'name':123,'age':18,'hight':189}
wpon= {'sex':'boy'}
dict.update(wpon)
dict.get('name')
print(dict.get('name'))
输出结果:
123

或者

dict= {'name':123,'age':18,'hight':189}
wpon= {'sex':'boy'}
dict.update(wpon)
dict.get('name')
print(dict['name'])
输出结果:
123

扩展

取出整个字典的key值

dict= {'name':123,'age':18,'hight':189}
wpon= {'sex':'boy'}
dict.update(wpon)
wpon2=dict.keys()
print(wpon2)
输出结果:
dict_keys(['name', 'age', 'hight', 'sex'])

value值同上, 把keys替换掉即可

dict = {'name':123,'age':18,'hight':189} 
wpon = {'sex':'boy'} 
dict.update(wpon) 
wpon2 = dict.keys() 
print(wpon2)
输出结果:
dict_values([123, 18, 189, 'boy'])
目录
相关文章
|
22天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
1月前
|
存储 索引 Python
python字典:怎么取出key对应的值
python字典:怎么取出key对应的值
37 0
|
1天前
|
JSON 数据格式 索引
python 又一个点运算符操作的字典库:Munch
python 又一个点运算符操作的字典库:Munch
10 0
|
2天前
|
存储 设计模式 算法
|
2天前
|
存储 索引 Python
|
13天前
|
安全 Python
python字典的内置方法
Python字典主要方法包括:`keys()`(返回所有键)、`values()`(返回所有值)、`items()`(返回所有键值对)、`get()`(安全取值,键不存在时返回默认值)、`setdefault()`(设置默认值)、`update()`(合并字典)、`pop()`(删除并返回值)、`clear()`(清空字典)、`copy()`(浅拷贝)、`fromkeys()`(新建字典并设置默认值)、`popitem()`(随机删除键值对)。
8 0
|
22天前
|
存储 Java 程序员
【Python】6. 基础语法(4) -- 列表+元组+字典篇
【Python】6. 基础语法(4) -- 列表+元组+字典篇
41 1
|
22天前
|
存储 Python
python基础篇: 详解 Python 字典类型内置方法
python基础篇: 详解 Python 字典类型内置方法
28 1
|
27天前
|
C语言 Python
Python字典推导式:高效构建字典的利器
在Python编程中,字典推导式(Dictionary Comprehension)是一种强大的构造工具,它允许我们以简洁的方式从现有可迭代对象创建新的字典。通过字典推导式,我们可以轻松地对数据进行转换、过滤或重新组织,以符合特定的需求。本文将深入探讨字典推导式的概念、语法和应用场景,帮助读者更好地掌握这一高效的编程工具。
|
1月前
|
Python
深入解析Python中的字典推导式
深入解析Python中的字典推导式

热门文章

最新文章