Guest User

Untitled

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