99% 教程不会讲的技巧,Python 字典推导式终极指南,小白也能秒懂

360影视 国产动漫 2025-05-31 07:58 2

摘要:# 目标:创建一个字典,键是数字 1-5,值是键的平方# 传统方法squares = {}for num in range(1, 6):squares[num] = num ** 2print(squares) # 输出: {1: 1, 2: 4, 3: 9,

字典推导式是 Python 中创建字典的一种高效语法,它允许你在一行代码内完成循环、条件判断和字典构建。

为什么需要字典推导式?

传统方法创建字典需要多行循环代码,而推导式可以让代码更简洁、更易读。

基础语法:

python

{键表达式: 值表达式 for 变量 in 可迭代对象 if 条件}

示例 1:用数字生成字典

python

# 目标:创建一个字典,键是数字 1-5,值是键的平方# 传统方法squares = {}for num in range(1, 6):squares[num] = num ** 2print(squares) # 输出: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}# 字典推导式方法squares = {num: num ** 2 for num in range(1, 6)}print(squares) # 输出: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

分步解释:

num: num ** 2:定义键值对,左边是键,右边是值for num in range(1, 6):遍历 1 到 5 的数字整体效果:每次循环生成一个键值对,最终组合成完整字典

示例 2:过滤偶数键

python

# 目标:创建字典,键是 1-10 的数字,值是键的平方,但只包含偶数键# 传统方法even_squares = {}for num in range(1, 11):if num % 2 == 0:even_squares[num] = num ** 2print(even_squares) # 输出: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}# 字典推导式方法even_squares = {num: num ** 2 for num in range(1, 11) if num % 2 == 0}print(even_squares) # 输出: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}

分步解释:

if num % 2 == 0:在遍历过程中只选择偶数只有满足条件的数字才会被加入到字典中

示例 3:列表转字典

python

# 目标:将两个列表合并成一个字典,一个列表作为键,另一个作为值names = ["Alice", "Bob", "Charlie"]ages = [25, 30, 35]# 传统方法people = {}for i in range(len(names)):people[names[i]] = ages[i]print(people) # 输出: {'Alice': 25, 'Bob': 30, 'Charlie': 35}# 字典推导式方法people = {names[i]: ages[i] for i in range(len(names))}print(people) # 输出: {'Alice': 25, 'Bob': 30, 'Charlie': 35}# 更优雅的方法:使用 zip 函数people = {name: age for name, age in zip(names, ages)}print(people) # 输出: {'Alice': 25, 'Bob': 30, 'Charlie': 35}

zip 函数解释:

zip(names, ages) 返回一个元组迭代器:[('Alice', 25), ('Bob', 30), ('Charlie', 35)]name, age 解包每个元组为变量

示例 4:生成带计算的值

python

# 目标:创建字典,键是数字 1-5,值是包含键的平方和立方的列表# 字典推导式方法math_dict = {num: [num ** 2, num ** 3] for num in range(1, 6)}print(math_dict)# 输出: {1: [1, 1], 2: [4, 8], 3: [9, 27], 4: [16, 64], 5: [25, 125]}

分步解释:

示例 5:嵌套列表转字典

python

# 目标:将嵌套列表转换为字典,每个子列表的第一个元素作为键,其余元素作为值data = [["apple", "red", "sweet"],["banana", "yellow", "soft"],["grape", "purple", "sweet"]]# 字典推导式方法fruits = {item[0]: item[1:] for item in data}print(fruits)# 输出: {# 'apple': ['red', 'sweet'],# 'banana': ['yellow', 'soft'],# 'grape': ['purple', 'sweet']# }

示例 6:根据条件生成不同的值

python

# 目标:创建字典,键是 1-5 的数字,偶数对应的值为 "even",奇数对应的值为 "odd"# 字典推导式方法result = {num: "even" if num % 2 == 0 else "odd" for num in range(1, 6)}print(result) # 输出: {1: 'odd', 2: 'even', 3: 'odd', 4: 'even', 5: 'odd'}

条件表达式语法:

python

值1 if 条件 else 值2如果条件为真,返回值 1如果条件为假,返回值 2

示例 7:字典值的转换

python

# 目标:将字典中的所有值转换为字符串类型original = {"a": 1, "b": 2, "c": 3}# 字典推导式方法string_dict = {key: str(value) for key, value in original.items}print(string_dict) # 输出: {'a': '1', 'b': '2', 'c': '3'}

分步解释:

original.items 返回键值对元组:[('a', 1), ('b', 2), ('c', 3)]key: str(value) 将每个值转换为字符串类型

错误 1:键重复

python

# 错误示例:重复的键会导致后面的覆盖前面的keys = ["a", "b", "a"]values = [1, 2, 3]result = {k: v for k, v in zip(keys, values)}print(result) # 输出: {'a': 3, 'b': 2}

错误 2:语法错误

python

# 错误:缺少冒号# wrong_dict = {x x*2 for x in range(5)} # 会报错# 正确:correct_dict = {x: x*2 for x in range(5)}

测试代码:

python

import timeit# 传统循环方法def traditional:result = {}for i in range(1000):result[i] = i ** 2return result# 字典推导式方法def comprehension:return {i: i ** 2 for i in range(1000)}# 测试性能time_traditional = timeit.timeit(traditional, number=1000)time_comprehension = timeit.timeit(comprehension, number=1000)print(f"传统方法耗时: {time_traditional:.4f} 秒")print(f"推导式方法耗时: {time_comprehension:.4f} 秒")# 输出示例:# 传统方法耗时: 0.2456 秒# 推导式方法耗时: 0.1872 秒

性能结论:

掌握字典推导式可以让我们的 Python 代码更加 Pythonic,提升开发效率和代码质量!

来源:小圆科技观

相关推荐