Python字典核心用法:创建、操作、视图与生成式详解

Oliver
Oliver
发布时间:2026-10-14| 5人浏览过
原创

本文深入解析Python中字典的核心用法,涵盖使用花括号和dict()函数创建字典、通过键获取元素、执行增删改操作、获取keys/values/items视图、遍历字典以及使用字典生成式等关键技能。

引言

在Python中,列表和字典均属于可变序列类型,支持增加、删除、修改及排序等多种操作。

与列表不同,字典是无序的数据结构。

一、字典的创建


#使用{}创建
scores = {'张三':100 ,'李四':98 ,'王麻子':72}
print(scores)
print(type(scores))
#使用内置函数dict()
student = dict(name = 'jack ', age = 16)
print(student)
print(type(student))

二、字典元素的操作

(一)获取


#获取字典中的元素
#方法一:
print(scores['张三'])
#方法二:
print(scores.get('张三'))
print(scores.get('66'))
#如果查找的不存在,返回none

(二)增删改

删除操作


del scores['张三'] #根据索引删除 key 和value
print(scores)
scores.clear()  #删除所有
print(scores)

新增操作(直接增加)


scores['赵四']  = 80

三、获取字典的视图


# 获取所有key值
key = scores.keys()
print(key)
print(type(key))
print(list(key)) #将key组成的视图转成list
#获取所有value值
value = scores.values()
print(value)
print(type(value))
print(list(value)) #将value组成的视图转成list
#获取所有的key-value值
items = scores.items()
print(items)
print(type(items))
print(list(items)) #转换为list后元素由元组组成

四、字典的遍历


for item in scores :
    print(item,end=' ') #输出的是字典中的key
    #输出key对应的value
    print(scores[item],end=' ')
    print(scores.get(item))

五、字典的特点

六、字典生成式


students = ['mark','sheep','jerry','tom']
grades = [100,78,60,59]
d={key:price for key,price in zip(students,grades)}
print(d)

总结

本文到此结束,希望能为读者提供帮助,同时也欢迎持续关注本站以获取更多技术内容。

引言

在Python中,列表和字典均属于可变序列类型,支持增加、删除、修改及排序等多种操作。

与列表不同,字典是无序的数据结构。

一、字典的创建


#使用{}创建
scores = {'张三':100 ,'李四':98 ,'王麻子':72}
print(scores)
print(type(scores))
#使用内置函数dict()
student = dict(name = 'jack ', age = 16)
print(student)
print(type(student))

二、字典元素的操作

(一)获取


#获取字典中的元素
#方法一:
print(scores['张三'])
#方法二:
print(scores.get('张三'))
print(scores.get('66'))
#如果查找的不存在,返回none

(二)增删改

删除操作


del scores['张三'] #根据索引删除 key 和value
print(scores)
scores.clear()  #删除所有
print(scores)

新增操作(直接增加)


scores['赵四']  = 80

三、获取字典的视图


# 获取所有key值
key = scores.keys()
print(key)
print(type(key))
print(list(key)) #将key组成的视图转成list
#获取所有value值
value = scores.values()
print(value)
print(type(value))
print(list(value)) #将value组成的视图转成list
#获取所有的key-value值
items = scores.items()
print(items)
print(type(items))
print(list(items)) #转换为list后元素由元组组成

四、字典的遍历


for item in scores :
    print(item,end=' ') #输出的是字典中的key
    #输出key对应的value
    print(scores[item],end=' ')
    print(scores.get(item))

五、字典的特点

六、字典生成式


students = ['mark','sheep','jerry','tom']
grades = [100,78,60,59]
d={key:price for key,price in zip(students,grades)}
print(d)

总结

本文到此结束,希望能为读者提供帮助,同时也欢迎持续关注本站以获取更多技术内容。

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。