Python3 内置函数
The 30 builtins that replace hand-written loops, with what each one actually does.
Key PointsLearn `any`/`all`/`zip`/`enumerate`/`sorted(key=)`/`sum(generator)` — they replace most manual loops and read better.
Iteration Helpers
| Function | Does | Example |
|---|---|---|
| enumerate(it, start) | Index + item pairs | list(enumerate('ab', 1)) |
| zip(a, b) | Pair up, stops at shortest | list(zip([1,2], 'ab')) |
| map(f, it) | Lazy transform | list(map(str, [1,2])) |
| filter(f, it) | Keep where f is truthy | list(filter(None, [0,1,2])) |
| reversed(seq) | Reverse iteration | list(reversed([1,2,3])) |
| range(a, b, s) | Numeric sequence | list(range(0, 10, 3)) |
Aggregation
| Function | Does | Note |
|---|---|---|
| sum(it, start=0) | Add numbers | Works on generators |
| min / max(it, key=) | Extremes | key= compares a projection |
| any(it) | True if any truthy | Short-circuits |
| all(it) | True if all truthy | Empty -> True |
| len / abs / round | Basics | round uses banker's rounding |
| sorted(it, key=, reverse=) | New sorted list | Stable sort |
Python
nums = [3, 1, 4, 1, 5]
print(sum(nums), max(nums), min(nums))
print(any(n > 4 for n in nums), all(n > 0 for n in nums)) # True True
print(sorted(nums, reverse=True)[:2]) # [5, 4]
# 找最大值的对象(而不是最大值本身)
rows = [{"n": "a", "v": 3}, {"n": "b", "v": 9}]
print(max(rows, key=lambda r: r["v"])["n"]) # bType and Object Helpers
| Function | Does |
|---|---|
| isinstance(x, T) | Type check, supports tuples of types |
| type(x) | Exact type object |
| hasattr / getattr / setattr | Dynamic attribute access |
| id(x) | Identity (memory address) |
| repr(x) / str(x) | Debug text / display text |
| hash(x) | Hash value, needs __hash__ |
| callable(x) | Is it callable |
| iter / next | Manual iteration |
Python
isinstance(True, int) # True —— bool 是 int 的子类
getattr(obj, "name", "n/a") # 带默认值,属性缺失不报错
it = iter([1, 2])
next(it), next(it, None) # (1, 2)Functional Building Blocks
Python
from functools import reduce
reduce(lambda a, b: a * b, [1, 2, 3, 4]) # 24
list(map(lambda x: x * 2, [1, 2, 3])) # [2, 4, 6]
# 推导式通常比 map/filter 更 Pythonic
[x * 2 for x in [1, 2, 3]]map/filter return lazy iterators, not lists. Printing them shows <map object at 0x...> — wrap in list() when you need to inspect.迭代辅助
| 函数 | 作用 | 示例 |
|---|---|---|
| enumerate(it, start) | 下标 + 元素 | list(enumerate('ab', 1)) |
| zip(a, b) | 配对,按最短截断 | list(zip([1,2], 'ab')) |
| map(f, it) | 惰性变换 | list(map(str, [1,2])) |
| filter(f, it) | 保留 f 为真的元素 | list(filter(None, [0,1,2])) |
| reversed(seq) | 反向迭代 | list(reversed([1,2,3])) |
| range(a, b, s) | 数字序列 | list(range(0, 10, 3)) |
聚合
| 函数 | 作用 | 备注 |
|---|---|---|
| sum(it, start=0) | 求和 | 可接生成器 |
| min / max(it, key=) | 取极值 | key= 按投影比较 |
| any(it) | 任一为真 | 短路求值 |
| all(it) | 全部为真 | 空序列返回 True |
| len / abs / round | 基础 | round 是银行家舍入 |
| sorted(it, key=, reverse=) | 返回新列表 | 稳定排序 |
Python
nums = [3, 1, 4, 1, 5]
print(sum(nums), max(nums), min(nums))
print(any(n > 4 for n in nums), all(n > 0 for n in nums)) # True True
print(sorted(nums, reverse=True)[:2]) # [5, 4]
# 找最大值的对象(而不是最大值本身)
rows = [{"n": "a", "v": 3}, {"n": "b", "v": 9}]
print(max(rows, key=lambda r: r["v"])["n"]) # b类型与对象辅助
| 函数 | 作用 |
|---|---|
| isinstance(x, T) | 类型判断,可传类型元组 |
| type(x) | 精确类型对象 |
| hasattr / getattr / setattr | 动态属性访问 |
| id(x) | 身份(内存地址) |
| repr(x) / str(x) | 调试文本 / 展示文本 |
| hash(x) | 哈希值,需要 __hash__ |
| callable(x) | 是否可调用 |
| iter / next | 手动迭代 |
Python
isinstance(True, int) # True —— bool 是 int 的子类
getattr(obj, "name", "n/a") # 带默认值,属性缺失不报错
it = iter([1, 2])
next(it), next(it, None) # (1, 2)函数式积木
Python
from functools import reduce
reduce(lambda a, b: a * b, [1, 2, 3, 4]) # 24
list(map(lambda x: x * 2, [1, 2, 3])) # [2, 4, 6]
# 推导式通常比 map/filter 更 Pythonic
[x * 2 for x in [1, 2, 3]]map/filter 返回惰性迭代器,不是列表。直接 print 只会看到 <map object at 0x...>,要查看请套 list()。