【Python】使用多个迭代器
2024-08-26 02:11:54
如果要达到多个迭代器的效果,__iter__()只需替迭代器定义新的状态对象,而不是返回self
class SkipIterator:
def __init__(self, wrapped):
self.wrapped = wrapped
self.offset = 0 def __next__(self):
if self.offset >= len(self.wrapped):
raise StopIteration
else:
item = self.wrapped[self.offset]
self.offset+=2
return item class SkipObject:
def __init__(self, wrapped):
self.wrapped = wrapped def __iter__(self):
return SkipIterator(self.wrapped) if __name__ == '__main__':
hhh = 'hahaha'
skipper = SkipObject(hhh)
for i in skipper:
print(i) I = iter(skipper)
print(next(I), next(I), next(I))
参考资料:Python学习手册
最新文章
- TCP/IP 七层协议
- 利用cubieboard设置samba打印服务器
- STM32 DAC的配置与使用
- redis的主从复制部署和使用
- C# Windows Forms 事件处理顺序
- linux-阿里云ECS部署PPTP(centos)
- LeetCode Balanced Binary Tree (判断平衡树)
- 博客终于开通了happy
- anroid里面的post请求
- debounce去弹跳
- C# Value type vs Reference type
- mysql(4)—— 表连接查询与where后使用子查询的性能分析。
- 利用 xrandr 命令修改屏幕分辨率
- [APIO2008]免费道路(生成树)
- 170814、Java使用gzip压缩文件、还原文件
- RW RO ZI ROM keil中的含义
- ";Error: ANDROID_HOME is not set and ";android"; command not in your PATH. You must fulfill at least one of these conditions.";.
- 【react读取文件】react发送GET请求读取静态文件
- git推送到github报错:error: The requested URL returned error: 403 Forbidden while accessing https://github.com
- Zabbix 3.4.11版本 自定义监控项