An object that represents a stream of data. It fetches one element at a time using the __next__()
method and maintains its internal state to know "what's next."
range
enumerate
zip
map
filter
reversed
Iterator vs. Sequence| Function | Return Type | Reusable? | Indexing/Slicing? |
|---|---|---|---|
range() |
Sequence | Yes | Yes |
enumerate() |
Iterator | No | No |
zip() |
Iterator | No | No |
map() |
Iterator | No | No |
filter() |
Iterator | No | No |
reversed() |
Iterator | No | No |
Sequence: you can loop through it multiple times, and it supports indexing and slicing, while being
memory efficient.
Iterator: you can loop through it only once, and it's consumed and becomes empty; if you need the
data again, you must convert it into a lasting type or recreate the object.
A special type of iterator that allows you to loop over data without storing the entire dataset in
memory. It generates values on the fly (lazily) using yield, making it extremely memory-efficient for
processing large files or streams.
yield
| 特性 | Iterator | Generator |
|---|---|---|
| 定义 | 遵循迭代协议的对象。 | 基于函数的特殊迭代器。 |
| 实现方式 | 基于类(使用 __iter__ 和 __next__)。 |
基于函数(使用 yield 关键字)。 |
| 状态管理 | 手动管理(通过类实例变量记录位置)。 | 自动管理(在 yield 处自动暂停/恢复)。 |
| 内存占用 | 低(惰性求值,按需生成数据)。 | 低(惰性求值,按需生成数据)。 |
| 复杂度 | 高(需要编写较多样板代码)。 | 低(语法简洁,可读性强)。 |
| 典型用途 | 自定义复杂对象或数据结构。 | 处理数据流、简单序列或流水线计算。 |
A concise, readable syntax for creating new collections by iterating over existing ones.
list
dict
set
Features |
列表推导式 [x for x in data] |
生成器表达式 (x for x in data) |
|---|---|---|
| 内存占用 | 随数据量 线性增长(一次性存入内存) | 恒定不变(仅保存计算逻辑) |
| 执行速度 | 遍历小规模数据时更快 | 在需要提前退出(如 any)时效率更高 |
| 可重复性 | 可以多次访问、切片、索引 | 一次性对象,遍历完就枯竭 |
| 结果类型 | 返回一个 list 对象 |
返回一个 generator 对象 |
Situations |
列表推导式 [] |
生成器表达式 () |
|---|---|---|
| 需要反复使用结果 | 是(列表可多次访问) | 否(一次性消耗) |
需要索引或切片 (如 res[0]) |
是 | 否 |
| 处理超大数据集(百万级以上) | 否(容易内存溢出) | 是(内存占用极低) |
仅作为函数的参数(如 sum) |
可选,但效率低 | 最佳实践 |
A standard Python library module providing a collection of efficient, memory-friendly tools for creating and manipulating iterators.
count
cycle
repeat
chain
islice
itertools.
__iter__ + yield__next__.
__iter__ + __next__
__getitem__
__iter__ + __reversed__
In Python, an object is considered Hashable if it meets the following three conditions:
hash() value, via __hash__ implementation,
remains constant during its entire
lifetime.
__eq__ method to allow equality
checks.
a == b is true, then hash(a)
must equal hash(b).
dict keys or set elements.
| 数据类型 | 是否可哈希 | 原因 / 备注 |
|---|---|---|
int, float, str, bool |
是 | 原子级不可变类型。对象创建后值无法修改。 |
tuple |
取决于内容 | 只有当元组内的所有元素也都是可哈希时,元组才可哈希。 |
list, dict, set |
否 | 可变容器。内容可以随时修改,这会导致哈希值失效。 |
frozenset |
是 | 集合的不可变版本,专门设计为可哈希。 |
__eq__, you must also override __hash__; otherwise, the class becomes unhashable.
dataclass@dataclass(frozen=True) to automatically make a class hashable.