首页 > 编程语言 >python(13)--字典(Dict)

python(13)--字典(Dict)

时间:2023-02-17 14:04:03浏览次数:38  
标签:10 13 flower python rose -- print orchid 字典


 一、字典的基本操作

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

python(13)--字典(Dict)_python_02​编辑

2.建立空字典

实例:

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

python(13)--字典(Dict)_删除元素_03

python(13)--字典(Dict)_python_04

python(13)--字典(Dict)_python_05​编辑

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)_删除元素_06

python(13)--字典(Dict)_删除元素_07

python(13)--字典(Dict)_数据结构_08​编辑

如果有两个“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_09

python(13)--字典(Dict)_数据结构_10

python(13)--字典(Dict)_删除元素_11​编辑

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

4.增加字典元素 

实例:

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

print("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_删除元素_12

python(13)--字典(Dict)_删除元素_13

 5.更改元素内容

实例:

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

python(13)--字典(Dict)_数据结构_14

python(13)--字典(Dict)_数据结构_15

python(13)--字典(Dict)_python_16​编辑

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

删除元素实例: 

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

python(13)--字典(Dict)_数据结构_17

python(13)--字典(Dict)_数据结构_18

python(13)--字典(Dict)_删除元素_19​编辑

 删除字典实例:

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

python(13)--字典(Dict)_数据结构_20

python(13)--字典(Dict)_数据结构_21

7. 字典的复制

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

python(13)--字典(Dict)_数据结构_22

python(13)--字典(Dict)_python_23

 二、遍历字典

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)_数据结构_24

python(13)--字典(Dict)_python_25​编辑

2.遍历字典的键(key) 

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

python(13)--字典(Dict)_python_26

python(13)--字典(Dict)_删除元素_27​编辑

没有keys()函数也行:

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

python(13)--字典(Dict)_删除元素_28

python(13)--字典(Dict)_数据结构_29

python(13)--字典(Dict)_python_30​编辑

 3.遍历字典的值(value)

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

python(13)--字典(Dict)_python_31

python(13)--字典(Dict)_数据结构_32

python(13)--字典(Dict)_删除元素_33​编辑

 4.字典里面放字典

实例:人物介绍

role={

'鲁班':{
'技能':'土木建筑',
'职业':'工匠'
},
'钟无艳':{
'技能':'出谋划策',
'职业':'中国古代四大丑女之一'
},
'蔡文姬':{
'技能':'琴棋书画',
'职业':'董祀之妻'
}

}
for a,b in role.items():
print("姓名:",a)
print("介绍:",b)

python(13)--字典(Dict)_删除元素_34

python(13)--字典(Dict)_python_35

三、简单介绍下函数

len():求元素个数


get():搜寻字典的key

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


pop():删除元素

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



标签:10,13,flower,python,rose,--,print,orchid,字典
From: https://blog.51cto.com/hwuu/6064023

相关文章

  • 智能建筑中高低压配电设备集成和管理系统应用分析
    罗轩志安科瑞电气股份有限公司上海嘉定201801[摘要]电力智能监控系统是信息时代的产物,反映了人类在高效经济时代对生活质量的不断追求,美好的希望和简化的工作程序。当前,正......
  • 浅析智能消防应急照明和疏散指示系统在工业建筑项目上的应用
    罗轩志安科瑞电气股份有限公司上海嘉定201801摘要:随着我国社会经济的迅猛发展与城市化建设进程的加快,大型城市综合体建筑越来越多,随之而来的消防安全管理问题不容忽视。智......
  • ASEMI供应FGH60N60SMD安森美FGH60N60SMD车规级IGBT
    编辑:llASEMI代理FGH60N60SMD安森美FGH60N60SMD车规级IGBT型号:FGH60N60SMD品牌:ON/安森美封装:TO-247最大漏源电流:60A漏源击穿电压:600VRDS(ON)Max:mΩ引脚数量:3特性:IGBT二极管芯片......
  • 浅析物联网技术在公共建筑能源管理系统中的应用
    罗轩志安科瑞电气股份有限公司上海嘉定201801摘要:在能源形势紧张的大趋势下,高能耗的大型公共建筑能源管理系统的建设逐渐受到重视,以物联网技术及基础的建筑能源管理平台可......
  • 【Linux系统】Centos7系统下配置samba共享
    (【Linux系统】Centos7系统下配置samba共享)一、Samba服务介绍Samba是一套使用SMB(ServerMessageBlock)协议的应用程序,通过支持这个协议,Samba允许Linux服务器与Win......
  • ChatGPT会让6个低端岗位失业
    我是卢松松,点点上面的头像,欢迎关注我哦!其实最近我们也是研究探索ChatGPT的商业应用方向:比如账号买卖、卖文章、论文、脚本文案、卖使用次数和时长、OEM系统等等。目前来看,Ch......
  • 浅谈高压小车开关柜中无线测温系统设计与应用
     罗轩志安科瑞电气股份有限公司上海嘉定201801 摘要:文章提出了一种用于小车式开关柜的无线测温系统,包括设于小车动触头套管上的温度传感器、环绕设于小车动触头上的电流......
  • java二维数组
    1.查找1)顺序查找SeqSearch.java2)二分查找【二分法,放在算法讲解】2.顺序查找有一个数列:白眉鹰王、金毛狮王、紫衫龙王、青翼蝠王猜数游戏:从键盘中任意输入一个名称,判......
  • 浅述综合管廊供配电系统设计方案
    罗轩志安科瑞电气股份有限公司上海嘉定 201801 摘要:城市综合管廊的供配电系统设计多种多样,通过对综合管廊自用负荷的分析及安全运行的因素考虑,并结合综合管廊设计案例,......
  • 御品膏方精选四川遂宁白芷,帮助国人以科学的方式实现健康目标
    随着广大公众健康意识的不断提升,食品安全成为了人们日常消费中关注的焦点话题。而对于市场主体的食品企业而言,做到诚信自律,严把产品质量关,守护消费者权益,也是自身赖以生存和......