字典里面的数据以键值对 的形式出现,字典数据和数据顺序没有关系,即字典不支持下标,无论数据如何变化,只需要按照对应的键的名字查找数据
In [1]: dict1={'name':'Tom','age':20,'gender':'男'}
In [3]: print(dict1)
{'name': 'Tom', 'age': 20, 'gender': '男'}
In [4]: print(type(dict1))
<class 'dict'>
# 创建空字典
In [5]: dict2={}
In [6]: print(type(dict2))
<class 'dict'>
In [7]: dict3=dict()
# 创建空字典
In [9]: print(type(dict3))
<class 'dict'>
In [12]: dict1={'name':'Tom','age':20,'gender':'男'}
# 增加不存在的键值对
In [14]: dict1['id']=1
In [15]: print(dict1)
{'name': 'Tom', 'age': 20, 'gender': '男', 'id': 1}
# 增加存在的键值对,即修改键值对的值
In [16]: dict1['name']='Jack'
In [17]: print(dict1)
{'name': 'Jack', 'age': 20, 'gender': '男', 'id': 1}
del()/del:删除字典或删除字典中指定键值对
clear():清空字典
In [18]: dict1={'name':'Tom','age':20,'gender':'男'}
In [19]: del dict1
# 删除字典dict1,之后再去打印,会报错:NameError: name 'dict1' is not defined
In [21]: print(dict1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-21-55169f1c8990> in <module>()
----> 1 print(dict1)
NameError: name 'dict1' is not defined
In [22]: dict1={'name':'Tom','age':20,'gender':'男'}
In [23]: del dict1['name']
In [24]: print(dict1)
{'age': 20, 'gender': '男'}
# 尝试删除不存在的键值对,此时会报错:KeyError: 'names'
In [25]: del dict1['names']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-25-db3e79036ad6> in <module>()
----> 1 del dict1['names']
KeyError: 'names'
# 使用clear()函数清空字典
In [26]: dict1.clear()
In [27]: print(dict1)
{}
In [4]: dict1={'name':'Tom','age':20,'gender':'男'}
# 修改name为Jack
In [5]: dict1['name']='Jack'
# 当键值对不存在时就相当于添加操作
In [6]: dict1['id']='001'
In [7]: print(dict1)
{'name': 'Jack', 'age': 20, 'gender': '男', 'id': '001'}
In [9]: dict1={'name':'Tom','age':20,'gender':'男'}
# 查找name
In [10]: dict1['name']
Out[10]: 'Tom'
# 不存在则会报错:KeyError: 'names'
In [11]: dict1['names']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-11-87d98e1ada9e> in <module>()
----> 1 dict1['names']
KeyError: 'names'
字典序列.get(key,默认值)
如果当前查找的key不存在则返回第二个参数(默认值),如果省略第二个参数,则返回None
keys()函数:返回字典中所有key(可迭代对象)
values()函数:返回字典中的值(可迭代对象)
items()函数:返回键值对,以元组形式存放(可迭代对象)
# get()函数的使用
In [13]: dict1={'name':'Tom','age':20,'gender':'男'}
# 查找name
In [14]: dict1.get('name')
Out[14]: 'Tom'
# 查找names,names不存在,则返回默认值None
In [16]: print(dict1.get('names'))
None
# 修改参数,names不存在返回Lily
In [17]: dict1.get('names','Lily')
Out[17]: 'Lily'
# keys()函数的使用
In [18]: dict1.keys()
Out[18]: dict_keys(['name', 'age', 'gender'])
# values()函数的使用
In [19]: dict1.values()
Out[19]: dict_values(['Tom', 20, '男'])
# items()函数的使用
In [20]: dict1.items()
Out[20]: dict_items([('name', 'Tom'), ('age', 20), ('gender', '男')])
# 遍历字典key
In [21]: dict1={'name':'Tom','age':20,'gender':'男'}
In [22]: for key in dict1.keys():
...: print(key)
...:
name
age
gender
# 遍历字典value
In [23]: for value in dict1.values():
...: print(value)
...:
Tom
20
男
# 遍历字典元素
In [24]: for item in dict1.items():
...: print(item)
...:
('name', 'Tom')
('age', 20)
('gender', '男')
In [25]: for key,value in dict1.items():
...: print(f'{key}={value}')
...:
name=Tom
age=20
gender=男