- 字典是无序的可变序列,保存的内容是以“键-值对”的形式存放的。
- 通过键而不是通过索引来读取
- 字典是任意对象的无序集合
- 字典是可变的,并且可以任意嵌套
- 字典中的键必须唯一
- 字典中的键必须不可变
创建
一、创建字典时,在“键”和“值”之间使用冒号分隔,相邻两个元素使用逗号分隔,所有元素放在一对“{}”中:
dictionary = {'key1':'value1','key2':'value2',...,'keyn':'valuen',}
- dictionary:字典的名称
- key1,key2...keyn:元素的键,必须是唯一的,并且不可变。可以是字符串、数字或者元组
- value1,value2...valuen:元素的值,可以是任何数据类型,不是必须是唯一的。
二、创建空字典,其中dict()还可以通过已有数据快速创建字典:
dictionary = {} 或者 dictionary = dict()
1、通过映射函数创建字典
dictionary = dic(zip(list1,list2))
- dictionary:字典名称
- zip()函数:用于将多个列表或者元组对应位置的元素组合为元组,并返回包含这些内容的zip对象。如果想获取元组,可以通过tuple()函数;如果想获取列表,可以通过list()函数。
- list1:一个列表,用于指定要生成字典的键
- list2:一个列表,用于指定要生成字典的值
s1 = ['a','b','c','d']
s2 = ['A','B','C','D']
s = dict(zip(s1,s2))
print(s)
输出:
{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
2、通过给定的关键字参数创建字典
dictionary = dict(key1=value1,key2=value2,...,keyn=valuen)
- dictionary:字典名称
- key1,key2,...,keyn:参数名,必须是唯一的,并且符合python标识符的命名规则。该参数名会转换为字典的键
- value1,value2,...,valuen:参数值,可以是任何数据类型,不必须唯一。该参数值将被转换为字典的值
s = dict(a='A',b='B',c='C',d='D')
print(s)
输出:
{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
还可以使用dict对象的formkeys()方法创建值为空的字典:
dictionary = dict.fromkeys(list1)
- dictionary:字典名称
- list1:作为字典的键的列表
s1 = ['a','b','c','d']
s = dict.fromkeys(s1)
print(s)
输出:
{'a': None, 'b': None, 'c': None, 'd': None}
可以!通过已经存在的元组和列表创建字典:
s1 = ('a','b','c','d')
s2 = ['A','B','C','D']
s = {s1:s2}
print(s)
输出:
{('a', 'b', 'c', 'd'): ['A', 'B', 'C', 'D']}
不可以!将键的元组改成列表,再次创建字典:
s1 = ['a','b','c','d']
s2 = ['A','B','C','D']
s = {s1:s2}
print(s)
输出报错:
TypeError: unhashable type: 'list'
删除
1、del命令删除整个字典
del dictionary
2、只删除字典的全部元素,可以使用clear()方法实现。使用后,原字典转换为空字典
dictionary.clear()
s = ['A','B','C','D']
s.clear()
print(s)
输出:
[ ]
3、使用pop()方法删除并返回指定“键”的元素
s1 = ['A','B','C','D']
s=s1.pop(3)
print(s1)
输出:
['A', 'B', 'C']
4、使用popitem()方法删除并返回字典中的一个元素
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
s = s1.popitem()
print(s)
输出:
('d', 'D')
通过键值对访问字典
1、print(dictionary):需要确认键是否存在
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
print(s1['b'])
输出:
B
2、dictionary.get(key[,default])
- dictionary:字典对象,即要从中获取值的字典
- key:为指定的键
- default:为可选项,用于指定当指定的“键”不存在时,返回一个默认值,如果省略,则返回None
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
print(s1.get('b')) #s1.get('b')返回的是'B'
输出:
B
遍历字典
使用子弹对象的items()方法可以获取字典的“键-值对”列表:
dictionary.items()
- dictionary:字典对象,返回值为可遍历的(键-值对)元组列表。想要获取到具体的“键-值对”,可以通过for循环遍历该元组列表。
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
for item in s1.items():
print(item)
print("~~~~~")
for key,value in s1.items():
print(key,value)
输出:
('a', 'A')
('b', 'B')
('c', 'C')
('d', 'D')
~~~~~
a A
b B
c C
d D
添加字典元素
dictionary[key] = value
- dictionary:字典名称
- key:要添加元素的键,必须是唯一,并且不可变,可以是字符串、数字或者元组。
- value:元素的值,可以是任何数据类型,不是必须唯一的。
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
s1['e']='E'
print(s1)
输出:
{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E'}
修改字典元素
添加一个元素,当元素存在时,则相当于修改功能
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
s1['a']='E'
print(s1)
输出:
{'a': 'E', 'b': 'B', 'c': 'C', 'd': 'D'}
删除字典元素
使用del命令将其删除,需要判断键是否存在
s1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
del s1['a']
print(s1)
输出:
{'b': 'B', 'c': 'C', 'd': 'D'}
字典推导式
使用字典推导式可以快速生成一个字典,它的表现方式与列表推导式类似。举例:
import random
number = { i : random.randint(10,100) for i in range(1,5) }
print(number)
输出:
{1: 27, 2: 93, 3: 75, 4: 88}
s1 = ['a','b','c','d']
s2 = ['A','B','C','D']
number = { i : j+"123" for i,j in zip(s1,s2)}
print(number)
输出:
{'a': 'A123', 'b': 'B123', 'c': 'C123', 'd': 'D123'}