Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- # 对生成器使用切片
- list(itertools.islice(range(10),2,5))
- # itertools.islice(range(10),2,5) 返回的还是一个生成器,外面加上 list 就可以转换为 [2,3,4]
- # 读取文档内容时,过滤掉开头所有以 # 开头的几行(文档中间如果某行使用 # 开头不过滤)
- with open('/etc/passwd') as f:
- for line in dropwhile(lambda line: line.startswith('#'), f):
- print(line, end='')
- # dropwhile 函数第一个参数是一个判断函数,第二个参数是生成器。当第一个函数的值取 true 时,对应生成器的元素不会返回,直到出现第一个 False 为止
Add Comment
Please, Sign In to add comment