# Python——迭代器的几个高级用法

## 跳过开头

# This is a data for student
# Rows 100

xiaoming, 17, 99;
xiaoli, 18, 98;
...


with open('xxxx.txt') as f:
for line in f:
print(line)


dropwhile是itemtools当中的一个函数，它可以 接收一个我们自定义的过滤函数和迭代器重新生成一个新的迭代器 ，这个新的迭代器当中会过滤掉之前迭代器头部不符合我们要求的数据：

from itertools import dropwhile
with open('xxxx.txt') as f:
for line in dropwhile(lambda line: line.startswith('#'), f):
print(line)


from itertools import dropwhile
with open('xxxx.txt') as f:
for line in islice(f, 3, None):
print(line)


## 迭代排列组合

items = ['a', 'b', 'c']
from itertools import permutations

for p in permutations(items):
print(p)


permutations还支持多传一个参数，比如上述的排列当中我们希望只保留前两个元素，除了切片之外，我们只需要多传一个参数就好了，like this：

for p in permutations(items, 2):
print(p)


from itertools import combindations
for c in combinations(items):
print(c)


for c in combinations_with_replacement(items, 3):
print(c)


## 迭代合并后的序列

from itertools import chain
nums = [1, 2, 3]
chars = ['a', 'b', 'c']

for i in chain(nums, chars):
print(i)


for i in nums + chars:
print(i)


## 归并迭代的内容

a = [1, 3, 5]
b = [2, 4, 6]

import heapq

for c in heapq.merge(a, b):
print(c)