美文网首页
Python Day 03

Python Day 03

作者: 牧笛_ini | 来源:发表于2019-03-08 20:37 被阅读0次

Generator

An infinite list cannot be enumerated because of the limits of ram. But we could use the generator to identify the principles of list generation.

Note: use the generator as the List Comprehensions but replace the [] by ()

l=[x*(x-1) for x in rang(100)]
g=(x*(x-1) for x in rang(100))

l is a list while the g is a generator

how to call a generator

  • the function next() can help you obtain the next value of the generator
  • you can also obtain the value by for loop

相关文章

网友评论

      本文标题:Python Day 03

      本文链接:https://www.haomeiwen.com/subject/plpdpqtx.html