Files
python3-cookbook/cookbook/c04/p08_skip_iterable.py

25 lines
520 B
Python
Raw Permalink Normal View History

2014-09-17 00:35:30 +08:00
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 跳过可迭代对象开始部分
Desc :
"""
from itertools import dropwhile
from itertools import islice
def skip_iter():
# with open('/etc/passwd') as f:
2019-04-25 17:16:54 +08:00
# for line in dropwhile(lambda line: not line.startswith('#'), f):
2014-09-17 00:35:30 +08:00
# print(line, end='')
2019-04-25 17:16:54 +08:00
# 明确知道了要跳过的元素序号
2014-09-17 00:35:30 +08:00
items = ['a', 'b', 'c', 1, 4, 10, 15]
2019-04-25 17:16:54 +08:00
for x in islice(items, 3, None):
2014-09-17 00:35:30 +08:00
print(x)
if __name__ == '__main__':
skip_iter()