惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
T
Tenable Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
S
Secure Thoughts
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
MIT News - Artificial intelligence
T
Tor Project blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
D
Docker
J
Java Code Geeks
GbyAI
GbyAI
H
Heimdal Security Blog
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Register - Security
The Register - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 百衲本

Centos7 副本集模式部署 MongoDB centos7部署Nacos集群模式 centos7 yum快速安装clickhouse单机版 k8s高可用部署Seata kubernetes中pod磁盘占用排查 jumpserver V3 终端常用操作 Python 列表生成式、字典生成式与生成器表达式 python email模块自动化操作邮件 python yagmail 模块自动化操作邮件 python自动化操作PDF 国内企业邓白氏编码免费申请流程 Python psutil模块 免费公共API调用清单 Python pathlib 模块 容器网络故障排查:从 ping 到 tcpdump 的全链路思路 systemd详解 APP性能指标 Linux 上禁用 USB 存储设备 线上故障的排查清单,运维小哥拿走不谢!
Python 的可迭代对象、迭代器对象与生成器
百衲本 · 2025-09-30 · via 博客园 - 百衲本

一、可迭代对象 (Iterable)

定义:任何实现了__iter__()方法的对象,或者实现了__getitem__()方法且参数从0开始索引的对象

特点:可以被用于for循环,可以使用iter()函数获取其迭代器

# 常见的可迭代对象
my_list = [1, 2, 3]  # 列表
my_tuple = (1, 2, 3)  # 元组
my_string = "hello"  # 字符串
my_dict = {"a": 1, "b": 2}  # 字典
my_set = {1, 2, 3}  # 集合

# 使用for循环遍历
for item in my_list:
    print(item)

# 使用iter()函数
iter_obj = iter(my_list)  # 等同于  my_list.__iter__()
print(next(iter_obj))  # 等同于 iter_obj.__next__()
print(next(iter_obj))
print(next(iter_obj)) 

二、迭代器对象 (Iterator)

定义:实现了__iter__()和__next__()方法的对象

特点:迭代器本身也是可迭代对象,每次调用next()会返回下一个值,耗尽后会抛出StopIteration异常

适用场景:需要自定义复杂遍历逻辑的场景

# 自定义迭代器
class MyIterator:
    def __init__(self, max_value):
        self.max_value = max_value
        self.current = 0
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if self.current < self.max_value:
            self.current += 1
            return self.current - 1
        else:
            raise StopIteration

# 使用自定义迭代器
my_iter = MyIterator(5)
print(next(my_iter))  # 0
print(next(my_iter))  # 1
print(next(my_iter))  # 2

# 或者用for循环
for num in MyIterator(3):
    print(num)  # 0, 1, 2
	



示例1:自定义数字范围迭代器	
	class NumberRange:
    """自定义数字范围迭代器"""
    def __init__(self, start, end, step=1):
        self.current = start
        self.end = end
        self.step = step
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        current_value = self.current
        self.current += self.step
        return current_value

# 使用迭代器
print("数字范围迭代器:")
numbers = NumberRange(1, 6)  # 1到5
for num in numbers:
    print(num, end=" ")  # 输出: 1 2 3 4 5
	



#简化版
def my_range(start, stop, step=1):
   while start < stop:
       yield start
       start += step

for i in my_range(1,5,2):  # 1 3
    print(i)

三、生成器 (Generator)

定义:使用yield关键字或生成器表达式创建的特殊迭代器

特点:懒加载,按需生成值,内存效率高,执行到yield时暂停,下次从暂停处继续

适用场景:适合简单的数据流处理、懒加载和内存敏感的场景

3.1 生成器函数

def countdown(n):
    """生成器函数示例"""
    print("开始倒计时")
    while n > 0:
        yield n
        n -= 1
    print("发射!")

# 使用生成器
cd = countdown(5)
print(cd)  #实际指向了内存地址,并不存储真实数据 <generator object countdown at 0x000001CF42EEE2C0>
print(next(cd))  # 5
print(next(cd))  # 4
print(next(cd))  # 3

# 或者用for循环
for num in countdown(3):
    print(num)  # 3, 2, 1
	

3.2 生成器表达式 

# 类似于列表推导式,但使用圆括号
squares = (x*x for x in range(5)) # 类别生成式格式为 [x*x for x in range(5)] ,注意没有元组生成式,因为()给了生成器

print(next(squares))  # 0
print(next(squares))  # 1
print(next(squares))  # 4

# 或者转换为列表
result = list(squares)
print(result)  # [9, 16]	

使用场景和示例

场景1:处理大型数据集(内存优化)
python
def read_large_file(file_path):
    """逐行读取大文件,避免内存溢出"""
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            yield line.strip()

# 使用示例
for line in read_large_file('large_data.txt'):
    # 处理每一行数据
    if 'error' in line:
        print(line)
		
		
		
场景2:简单的计数器生成器
python
def simple_counter(limit):
    """简单的计数器生成器"""
    count = 1
    while count <= limit:
        yield count
        count += 1

# 使用生成器
print("\n计数器生成器:")
counter = simple_counter(5)
for num in counter:
    print(num, end=" ")  # 输出: 1 2 3 4 5