Code Monkey home page Code Monkey logo

scripts's Introduction

scripts

脚本练习

迭代:通过for循环遍历对象的每一个元素的过程

迭代器(Iterator)和可迭代(Iterable)的区别:

  • 凡是可作用于for循环的对象都是可迭代类型;
  • 凡是可作用于next()函数的对象都是迭代器类型;
  • list、dict、str等是可迭代的但不是迭代器,因为next()函数无法调用它们。可以通过iter()函数将它们转换成迭代器。
  • Python的for循环本质上就是通过不断调用next()函数实现的。

生成器(generator):

  • 节省内存,调用才占用内存
  • 一边循环一边计算出元素的机制,称为生成器
  • 可调用next()函数
  • yield
  • 生成器在 Python 的写法是用小括号括起来,(i for i in range(100000000)),即初始化了一个生成器。除了使用生成器推导式,我们还可以使用yield关键字

在 Python中,使用yield返回的函数会变成一个生成器(generator)。 在调用生成器的过程中,每次遇到yield时函数会暂停并保存当前所有的运行信息,返回yield的值。并在下一次执行next()方法时从当前位置继续运行。

scripts's People

Contributors

hwgin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.