每天CookBook之Python-064

  • 根据条件,跳过部分迭代器
from itertools import dropwhile

with open('passwd.txt') as f:
    for line in dropwhile(lambda  line: line.startswith('pass'), f):
        print(line)

from itertools import islice

items = ['a', 'b', 'c', 1, 4, 10, 15]
for x in islice(items, 3, None):
    print(x)

with open('passwd.txt') as f:
    while True:
        line = next(f, '')
        if not line.startswith('pass'):
            break
    while line:
        print(line)
        line = next(f, None)

with open('passwd.txt') as f:
    lines = (line for line in f if not line.startswith('pass'))
    for line in lines:
        print(line)

out

python3333

1
4
10
15
python3333

python3333
posted @ 2016-07-22 20:43  4Thing  阅读(118)  评论(0编辑  收藏  举报