An object capable of returning its members one at a time.
可迭代对象是一种能够一次返回其一个成员的对象。Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an () method or with a () method that implements sequence semantics.
可迭代对象的例子包括所有的序列类型(例如 list、str 和 tuple)和一些非序列类型,比如 dict、文件对象,以及任何定义了 iter() 方法或定义了实现序列语义的 getitem() 方法的类所创建的对象。
iter
getitem
'迭代'词义:汉语词汇,拼音为 dié dài,原指事物更相代替、轮换的过程,在计算机科学领域中,迭代通常涉及将每次迭代的结果作为下一次迭代的初始值,从而实现对问题的逐步求解。
# 错误示例
data = {"name": "Alice", "age": 25}
try:
for item in data["name"]: # 这里迭代的是字符串"Alice"print(item)
except Exception as e:
print(f"错误:{e}")
# 你可能想这样做:for key in data:
print(f"{key}: {data[key]}")
5). 不可迭代对象
就像一块石头,你无法从中'取出'多个部分。
a. 内置不可迭代对象
from collections.abc import Iterable
# 1. 整数 (int)
num = 42print(isinstance(num, Iterable)) # False# 尝试迭代会报错:TypeError: 'int' object is not iterable# 2. 浮点数 (float)
pi = 3.14159print(isinstance(pi, Iterable)) # False# 尝试迭代会报错:TypeError: 'float' object is not iterable# 3. 布尔值 (bool)
flag = Trueprint(isinstance(flag, Iterable)) # False# 尝试迭代会报错:TypeError: 'bool' object is not iterable# 4. Noneprint(isinstance(None, Iterable)) # False# 尝试迭代会报错:TypeError: 'NoneType' object is not iterable# 5. 函数对象defmy_func():
return"hello"print(isinstance(my_func, Iterable)) # False# 尝试迭代会报错:TypeError: 'function' object is not iterable# 6. 类对象classMyClass:
pass
obj = MyClass()
print(isinstance(obj, Iterable)) # False# 尝试迭代会报错:TypeError: 'MyClass' object is not iterable
b. 尝试迭代不可迭代对象
# 错误示例
number = 123try:
for digit in number: # TypeError: 'int' object is not iterableprint(digit)
except TypeError as e:
print(f"错误:{e}")
# 解决方案 1:转换为可迭代对象
number = 123for digit instr(number): # 将数字转换为字符串print(digit) # 输出:1, 2, 3# 解决方案 2:使用适当的容器
number = 123
digits = [int(d) for d instr(number)] # 转换为列表for digit in digits:
print(digit) # 输出:1, 2, 3
3. 可迭代对象的内部机制
Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), …).
可迭代对象可以在 for 循环中使用,也可以在需要序列的许多其他地方使用(例如 zip()、map() 等)。When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object.
当可迭代对象作为参数传递给内置函数 iter() 时,它会返回该对象的迭代器。This iterator is good for one pass over the set of values.
这个迭代器适用于对值集合进行一次遍历。When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself.
使用可迭代对象时,通常不需要自己调用 iter() 或处理迭代器对象。The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop.
for 语句会自动为你完成这些操作,它会创建一个临时的无名变量来在循环期间保存迭代器。
defdata_pipeline(source):
"""数据清洗管道"""# 每一步都是惰性的
cleaned = (item.strip() for item in source) # 去空格
filtered = (item for item in cleaned if item) # 过滤空值
transformed = (item.upper() for item in filtered) # 转大写return transformed
5. 从'集合思维'到'流思维'
# 集合思维:先准备好所有数据defprocess_files_collection(files):
all_data = []
for file in files:
withopen(file) as f:
all_data.append(f.read())
# 现在才开始处理...for data in all_data:
process(data)
# 流思维:边获取边处理defprocess_files_stream(files):
for file in files:
withopen(file) as f:
for line in f: # 迭代器,逐行处理
process(line) # 立即处理,不等待
6. 从'命令式'到'声明式'
# 命令式:详细描述如何做defget_top_students(students, n=3):
result = []
for student in students:
if student.score > 90:
result.append(student)
result.sort(key=lambda s: s.score, reverse=True)
return result[:n]
# 声明式:描述要什么(使用迭代器工具)defget_top_students_declarative(students, n=3):
returnsorted(
(s for s in students if s.score > 90), # 生成器表达式
key=lambda s: s.score,
reverse=True
)[:n]
An object representing a stream of data.
迭代器是表示数据流的对象。
Repeated calls to the iterator's next() method (or passing it to the built-in function next()) return successive items in the stream.
重复调用迭代器的 next() 方法(或将其传递给内置函数 next())会返回流中的连续项。
When no more data are available a StopIteration exception is raised instead.
当没有更多数据可用时,会引发 StopIteration 异常。
At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again.
此时,迭代器对象已经耗尽,任何对其 next() 方法的进一步调用都只会再次引发 StopIteration。
Iterators are required to have an iter() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted.
迭代器必须具有 iter() 方法,该方法返回迭代器对象本身,因此每个迭代器也是可迭代的,并且可以用在大多数接受可迭代对象的地方。
One notable exception is code which attempts multiple iteration passes.
一个值得注意的例外是尝试多次迭代的代码。
A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop.
容器对象(例如列表)每次传递给 iter() 函数或在 for 循环中使用时,都会生成一个全新的迭代器。
Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
对迭代器尝试这样做只会返回与之前迭代过程中相同的已耗尽的迭代器对象,使其看起来像一个空容器。
classSquares:
"""生成从 0 到 n-1 的平方数"""def__init__(self, n):
self.n = n
self.current = 0def__iter__(self):
returnself# 返回自身,因为自身就是迭代器def__next__(self):
ifself.current >= self.n:
raise StopIteration
result = self.current ** 2self.current += 1return result
squares = Squares(5)
for sq in squares:
print(sq) # 0, 1, 4, 9, 16
生成器与迭代器的关系:
import collections.abc
# 生成器是一种更简洁的迭代器实现方式defsquares_generator(n):
for i inrange(n):
yield i ** 2# 生成器函数返回一个生成器对象,它既是迭代器也是可迭代对象
gen = squares_generator(5)
print(isinstance(gen, collections.abc.Iterator)) # Trueprint(isinstance(gen, collections.abc.Iterable)) # Truefor sq in gen:
print(sq)
3. 生成器(generator)
A function which returns a generator iterator.
生成器函数是一种返回生成器迭代器的函数。
It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.
它看起来像一个普通函数,不同之处在于它包含 yield 表达式,用于生成一系列可在 for 循环中使用的值,或者可以一次一个地用 next() 函数获取。
4. 递归
通过函数调用自身来解决问题。
例如:
# 经典例子:计算阶乘deffactorial(n):
# 终止条件:最小套娃if n == 1:
return1# 递归步骤:打开当前套娃,发现里面有个小一号的套娃else:
return n * factorial(n - 1)
print(factorial(5)) # 120