一、字典的基本操作
1.定义字典
字典也是一个列表型的数据结构,字典的数据是用“{ }”装的(列表:[ ],元组:( )),字典的元素是一一对应的关系“key-value”。
格式:
Dictname={ key1:value1,...,key2:value2}
#value是任何的python的对象
#字典的元素数量也是用len()函数
多说无益,直接看例子比较清晰:
实例:
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
print(flower)
print(tea)
print("字典flower的元素数量是:",len(flower))
print("字典的数据类型:",type(tea))
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NThlZmY1OTkzMS5wbmc%3D.png?w=700)
编辑
2.建立空字典
实例:
print("``````````````````````````````````````````````````````````")
flower={}
flower['rose']=13
flower['orchid']=16
print(flower)
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi8yM2FhNDdhNDc5ZTU1NGE3Y2E4NDk1YTRmNzUzZThiZDM3M2VlYi5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NjRmNDQ4NDQwMy5wbmc%3D.png?w=700)
编辑
3.列出字典元素的值
格式:
flower【'rose'】
#注意列出字典元素的值要用中括号哦“[ ]”
#上面语句表达的意思是字典 flower 的 rose(key)的对应 10(value)值。
实例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
print("一支玫瑰的价钱是:",flower['rose'])
print("红茶一袋的价钱是:",tea['红茶'])
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi9kNjQ2NTczOTMyNDVkMmNlMDM3NjM2Yjg2MDU5MWRhNDYwNGEyMi5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NjNlYmU0ODI2OC5wbmc%3D.png?w=700)
编辑
如果有两个“rose”,两个“红茶”呢,元素对应的值(value)是哪个呢?
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8,'rose':15}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40,'红茶':13}
print("一支玫瑰的价钱是:",flower['rose'])
print("红茶一袋的价钱是:",tea['红茶'])
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi9iMzc0ZTY3NjJhYjE5YjU1MmRmNzM3OTNhNWU5MGUwN2YzNmM0NC5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2N2YxZWYxNDQ4NC5wbmc%3D.png?w=700)
编辑
如上所示,字典中的元素对应值被后面的值占领了。
4.增加字典元素
实例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
flower['tuilp']=13
print(flower)
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi8xMjhlMzhkNDI0MTkyOWVjYzNkNDYxNjcxZGUzZjVlNmI2ODY5Ni5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NjRmNTg0OTMwNi5wbmc%3D.png?w=700)
5.更改元素内容
实例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
flower['rose']=13
print(flower)
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi9lNzE4ZTc4NTM4ZjIzNGM1ODNhNjY2ZjAxOTk4N2E2ZmNjYWYxMi5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NjYwMDMxMjI0NS5wbmc%3D.png?w=700)
编辑
6.删除字典(特定元素)
删除元素实例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
del flower['rose']
print(flower)
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi8yNzQ2YWI4NjlkNWY1YTQ3OTY0NjIzZjg0Yjk1ZThjMzZhYmZkZS5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NjM0YTM3NjIzNS5wbmc%3D.png?w=700)
编辑
删除字典实例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
del flower
print(flower)
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi8wOGNmYjlkMzVhZTA5ZDJjZTgyNzcyMzg0MzMzNTQ4YTk2OTllMi5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NmJiNzM2NDg1Ni5wbmc%3D.png?w=700)
7. 字典的复制
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
copyflower=flower.copy()
print(flower)
print(copyflower)
print("``````````````````````````````````````````````````````````")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi9iMzY3NjUyMDRmYjBiNjkyOGM5NTExMjZlZTkyNjNkZWRiZDZlMi5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NmM0ZjkzODk1NC5wbmc%3D.png?w=700)
二、遍历字典
1.遍历字典的key-value
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers,price in flower.items():
print("花名:",flowers)
print("价格:",price)
print("\n")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NmNlOWE5ODA1OC5wbmc%3D.png?w=700)
编辑
2.遍历字典的键(key)
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower.keys():
print("花名:",flowers)
print("\n")
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NmU3ZWMxNDkxNS5wbmc%3D.png?w=700)
编辑
没有keys()函数也行:
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower:
print("花名:",flowers)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi8zOGVjNjk4OTAzN2U1NzBjMDA0NDQ5NDk4MjE4NWJjYzcxNmQzMS5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2ODBkZjI4ODk0MC5wbmc%3D.png?w=700)
编辑
3.遍历字典的值(value)
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower.values():
print("价格:",flowers)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi9jOTg3ZWY2MDU2ZjFiMDAwMzNkNzcxNzdlNzVkNzUzMTQ4OWNjYy5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NzJmNmM3NjIzOS5wbmc%3D.png?w=700)
编辑
4.字典里面放字典
实例:人物介绍
role={
'鲁班':{
'技能':'土木建筑',
'职业':'工匠'
},
'钟无艳':{
'技能':'出谋划策',
'职业':'中国古代四大丑女之一'
},
'蔡文姬':{
'技能':'琴棋书画',
'职业':'董祀之妻'
}
}
for a,b in role.items():
print("姓名:",a)
print("介绍:",b)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzLzIwMjMwMi8wNWEzZTU4NTM0YWE2Mzk1NjEzMDc1YTQyMWUyOWE3MjUxNTQzMi5naWY%3D.gif?w=700)
![python(13)--字典(Dict) python(13)--字典(Dict)](https://image.shishitao.com:8440/aHR0cHM6Ly9zMi41MWN0by5jb20vaW1hZ2VzL2Jsb2cvMjAyMzAyLzE3MTMyMDU0XzYzZWYwZWI2NzFhY2Y2MjcxMi5wbmc%3D.png?w=700)
三、简单介绍下函数
len():求元素个数
get():搜寻字典的key
格式:返回值=字典名.get('key')
pop():删除元素
格式:返回值=字典名.pop('key')