Python dict 字典方法

发布时间:2026/10/22 12:23:51
Python dict 字典方法 正文该篇仅作为个人笔记欢迎收藏。假如我们有一个字典对象dict1{name:Xiao Ming,age:18,gender:male,status:single}注意字典中的元素没有顺序之说。len() 方法len()方法获取字典对象中的元素个数。dict1{name:Xiao Ming,age:18,gender:male,status:single}print(len(dict1)) result: 4 dict.[key] 方法dict.[key]方法通过某个key值(键值)获取与其对应的某个value值(值)dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1[name]) result: Xiao Ming dict.[key] xxx方法1.当指定的条目字典中不存在时直接向字典中添加新条目不用去更新字典2.当指定的条目字典中存在时修改字典中的值举例1dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1)dict1[city]Xianprint(dict1) result: {name: Xiao Ming, age: 18, gender: male, status: single} {name: Xiao Ming, age: 18, gender: male, status: single, city: Xian} 举例2dict1{name:Xiao Ming,age:18,gender:male,status:single}dict1[age]26print(dict1) result: {name: Xiao Ming, age: 26, gender: male, status: single} updata() 方法updata()方法将字典dict2中的元素添加到字典dict1中dict1{name:Xiao Ming,age:18,gender:male,status:single}dict2{pet:cat}dict1.update(dict2)print(dict1) result: {name: Xiao Ming, age: 18, gender: male, status: single, pet: cat} dict.[key] xxx方法主要用于插入单个数据到字典中而updata()方法常用于一次性插入多个数据到字典中。del dict[key] 方法del dict[key]方法删除字典中对应key值的value值注意与列表的del()方法(超链接点击跳转)做区分。dict1{name:Xiao Ming,age:18,gender:male,status:single}deldict1[name]print(dict1) result: {age: 18, gender: male, status: single} pop() 方法pop()方法删除字典中对一个key值的value值dict1{name:Xiao Ming,age:18,gender:male,status:single}dict1.pop(name)print(dict1) result: {age: 18, gender: male, status: single} clear() 方法clear()方法删除字典中的所有条目dict1{name:Xiao Ming,age:18,gender:male,status:single}dict1.clear()print(dict1) result: {} items() 方法items()方法返回键值对tuple的列表dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1.items()) result: dict_items([(name, Xiao Ming), (age, 18), (gender, male), (status, single)]) keys() 方法keys()方法返回键的列表dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1.keys()) result: dict_keys([name, age, gender, status]) values() 方法values()方法返回值的列表dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1.values()) result: dict_values([Xiao Ming, 18, male, single]) key in dict 方法key in dict方法判断字典中的某个键值是否存在。dict1{name:Xiao Ming,age:18,gender:male,status:single}print(ageindict1) result: True dict.get(key) 方法dict.get(key)方法判断字典中的某个键值是否存在存在则返回键值若没有则返回Nonedict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1.get(age))print(dict1.get(income)) result: 18 None dcit.get(key)方法的好处在不确定字典中是否存在对于的键值时如果直接使用dict[key]会产生错误导致程序中断而使用dict.get(key)可以保证程序不中断继续执行。请看如下代码dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1[xiao xiao]) result: Traceback (most recent call last): File C:/Users/15025/Desktop/Intelligence/debug00.py, line 14, in module print(dict1[income]) KeyError: income dict.get()方法也可以指定当选取对象不存在时的返回值(之前提到默认不存在返回为None)。比如dict1{name:Xiao Ming,age:18,gender:male,status:single}print(dict1.get(income,False)) result: False 可以看到此时的返回值变为了False。事实上我们也可以通过直接使用dict.get(key)来得到类似的结果但是在使用之前我们需要做一下设定。dict1{name:Xiao Ming,age:18,gender:male,status:single}dict1.setdefault(xiao xiao,False)print(dict1[xiao xiao]) result: False 可以看到此时程序也没有报错而是返回了我们设定的结果。举例3从列表中快速创建集合并统计列表中对应元素出现的次数。list1[a,v,c,d,a]c{}foriinset(list1):c[i]list1.count(i)print(c) result: {d: 1, c: 1, v: 1, a: 2} 码字不易如果大家觉得有用请高抬贵手给一个赞让我上推荐让更多的人看到吧~

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询