1. 列表推导式#
传统写法:
squares = []
for x in range(10):
squares.append(x**2)推导式一行搞定:
squares = [x**2 for x in range(10)]2. 同时遍历索引和元素#
items = ["a", "b", "c"]
for i, item in enumerate(items):
print(f"{i}: {item}")3. 字典合并#
a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
merged = a | b # Python 3.9+
# {'x': 1, 'y': 3, 'z': 4}4. f-string 格式化#
name = "世界"
price = 49.9
print(f"你好,{name}!价格是 ¥{price:.2f}")5. 解包赋值#
# 交换变量
a, b = b, a
# 多值解包
first, *middle, last = [1, 2, 3, 4, 5]
# first=1, middle=[2,3,4], last=56. 使用 pathlib 处理路径#
from pathlib import Path
# 比 os.path 更直观
data_dir = Path.home() / "data"
data_dir.mkdir(exist_ok=True)7. defaultdict 简化计数#
from collections import defaultdict
counts = defaultdict(int)
for word in ["a", "b", "a"]:
counts[word] += 18. itertools 组合迭代#
from itertools import combinations
for a, b in combinations([1, 2, 3], 2):
print(a, b)
# (1,2) (1,3) (2,3)9. 装饰器计时#
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"{func.__name__} 耗时 {time.time() - start:.3f}s")
return result
return wrapper10. dataclass 省样板代码#
from dataclasses import dataclass
@dataclass
class User:
name: str
age: int
u = User("张三", 25)
print(u) # User(name='张三', age=25)这 10 个技巧覆盖了日常开发中的高频场景,熟练使用它们能让你写出更精炼的 Python 代码。