python(13)--字典(Dict)

简介: python(13)--字典(Dict)

一、字典的基本操作


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))


1c1b1809ea694d3ea747ab8fbf32d8e6.png

2.建立空字典

实例:

print("``````````````````````````````````````````````````````````")
flower={}
flower['rose']=13
flower['orchid']=16
print(flower)
print("``````````````````````````````````````````````````````````")



fcc25d8ea43f4dc39c7bbd2e26556773.png


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("``````````````````````````````````````````````````````````")


6923e68224554f87b252275723f94c61.png

如果有两个“rose”,两个“红茶”呢,元素对应的值(value)是哪个呢?

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8,'rose':15}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40,'红茶':13}
print("一支玫瑰的价钱是:",flower['rose'])
print("红茶一袋的价钱是:",tea['红茶'])
print("``````````````````````````````````````````````````````````")



425e16a34d2248f08f93ee092dbd6088.png

如上所示,字典中的元素对应值被后面的值占领了。

4.增加字典元素

实例:

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
flower['tuilp']=13
print(flower)
print("``````````````````````````````````````````````````````````")


f399c804a77c4df1aea42d9270cf7096.png


5.更改元素内容

实例:

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
flower['rose']=13
print(flower)
print("``````````````````````````````````````````````````````````")


3e4204048fb74273bdfdb3bc2b6e3f7d.png

6.删除字典(特定元素)

删除元素实例:

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'红茶':30,'绿茶':20,'茉莉花茶':40}
del flower['rose']
print(flower)
print("``````````````````````````````````````````````````````````")


12f003e401f94cdb984e112cc7ba8359.png

删除字典实例:


print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
del flower
print(flower)
print("``````````````````````````````````````````````````````````")


63028d794f27479bbbfb0a2a66a420a4.png


7. 字典的复制


print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
copyflower=flower.copy()
print(flower)
print(copyflower)
print("``````````````````````````````````````````````````````````")

f295e8f362bf42a99cfdaf6d18d78629.png

二、遍历字典

1.遍历字典的key-value


flower={'rose':10,
        'orchid':16,
        'carnation':8}
for flowers,price in flower.items():
    print("花名:",flowers)
    print("价格:",price)
    print("\n")


4a3b7aebe9f34ce48324a75e43d5fbc5.png

2.遍历字典的键(key)

flower={'rose':10,
        'orchid':16,
        'carnation':8}
for flowers in flower.keys():
    print("花名:",flowers)
    print("\n")

d1587299c383447b9ba8abfe101bcb21.png


没有keys()函数也行:

1. flower={'rose':10,
2. 'orchid':16,
3. 'carnation':8}
4. for flowers in flower:
5. print("花名:",flowers)


11470176bfbb421f806399cfd967116e.png


3.遍历字典的值(value)

1. flower={'rose':10,
2. 'orchid':16,
3. 'carnation':8}
4. for flowers in flower.values():
5. print("价格:",flowers)


22869169fe8b41d5aca42ec992358a8c.png

4.字典里面放字典

实例:人物介绍


role={
    '鲁班':{
        '技能':'土木建筑',
        '职业':'工匠'
    },
    '钟无艳':{
        '技能':'出谋划策',
        '职业':'中国古代四大丑女之一'
    },
    '蔡文姬':{
        '技能':'琴棋书画',
        '职业':'董祀之妻'
    }
}
for a,b in role.items():
    print("姓名:",a)
    print("介绍:",b)

9ce0fba3c8424d83a4334f58d7162bee.png


5.简单介绍下函数


len():求元素个数

get():搜寻字典的key

格式:返回值=字典名.get('key')

pop():删除元素

格式:返回值=字典名.pop('key')


目录
相关文章
|
12天前
|
存储 缓存 数据库连接
Python基础教程——字典(Dictionary)
Python基础教程——字典(Dictionary)
|
13天前
|
开发工具 Python
Python列表和字典前面为什么要加星号( )?_python一个 代表列表
Python列表和字典前面为什么要加星号( )?_python一个 代表列表
|
18天前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
2天前
|
存储 数据处理 Python
Python字典的常用操作详解
Python字典的常用操作详解
6 1
|
2天前
|
存储 Python
Python字典的定义与操作详解
Python字典的定义与操作详解
4 1
|
2天前
|
存储 算法 数据处理
字典在Python中的应用与实例
字典在Python中的应用与实例
12 1
|
3天前
|
存储 缓存 算法
Python字典
Python字典
|
4天前
|
存储 数据处理 Python
Python中的字典(Dictionary)类型:深入解析与应用
Python中的字典(Dictionary)类型:深入解析与应用
|
4天前
|
存储 Python
Python字典类型及其操作详解
Python字典类型及其操作详解
11 1
|
5天前
|
存储 缓存 数据库
Python中字典
Python中字典
10 2