`
ilicis
  • 浏览: 13401 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

(),[] in python

阅读更多
初学python 看到(),[]认为是tuple和list而已,作为一个数组一个不能修改一个可以修改。不过由于python函数式表达的能力给()[]赋予了更强大的能力
一个是列表领悟,list comprehension,
a = [x for x in range(100) if x%2==1] #1到100的奇数
b = (x for x in range(100) if x%2==1)


看上去都是1到100的奇数,不过a是把整个list都生成了,而b只是产生一个generator
由于python还是一门命令式语言。在某些时候,用[]的方法,会严格按照应用序求值,会浪费很多时间空间。
而用()的方法,只是生成一个generator,这样就成了惰性求值,用到一点计算一点,丢弃一点
关于generator,也可以写在自己定义的函数里,使用yield关键字,在yield处返回值,下一次再从上次返回的地方继续

def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x
        yield t.label
        for x in inorder(t.right):
            yield x


这是二叉树的中序遍历,像这种使用递归的方法的函数里,把函数转化成generator,直接用yield代替return就可以了
分享到:
评论

相关推荐

    Coding Games in Python

    A visual step-by-step guide to writing code in Python. Beginners and experienced programmers can use Python to build and play computer games, from mind-bending brainteasers to crazy action games with ...

    Causal Inference and Discovery in Python

    "Causal Inference and Discovery in Python" 本书《Causal Inference and Discovery in Python》旨在介绍机器学习和Pearlian Perspective下的因果推断和发现技术,通过使用DoWhy、EconML、PyTorch等库,帮助读者_...

    Programming in Python 3 A Complete Introduction to the Python Language

    The purpose of this book is to show you how to write Python programs in good idiomatic Python 3 style, and to be a useful reference for the Python 3 language after the initial reading. Although Python...

    《Python3程序开发指南 Programming in Python 3 》中英版+源代码

    《Python3程序开发指南 Programming in Python 3》是一本针对初学者和有一定编程基础的开发者设计的Python3教程。这本书全面介绍了Python3编程语言的核心概念、语法和高级特性,旨在帮助读者快速掌握Python3编程技能...

    Deep Learning in Python

    Deep Learning in Python: Master Data Science and Machine Learning with Modern Neural Networks written in Python, Theano, and TensorFlow (Machine Learning in Python) by LazyProgrammer English | March ...

    Coding project in python

    Using fun graphics and easy-to-follow instructions, Coding Projects in Python is a straightforward, visual guide that shows young learners how to build their own computer projects using Python, an ...

    thinking in python 编程思想

    《Thinking in Python》是Python编程领域的一本经典教程,由Bruce Eckel撰写,深受程序员喜爱。这本书深入浅出地介绍了Python编程语言的核心概念和思维方式,旨在帮助读者理解Python的哲学和设计原则,培养解决问题...

    Think in python中英文对照pdf文件

    《Think in Python》是一本由Allen B. Downey编著的经典Python编程教程,旨在帮助读者深入理解Python语言的本质和思维方式。这本书的特点是采用问题驱动的学习方法,通过解决实际问题来引导学习者掌握Python编程的...

    Building Skills in Python

    Building Skills in Python Building Skills in Python Building Skills in Python Building Skills in Python

    Thinking in Python#Python 编程思想

    《Thinking in Python》是Bruce Eckel撰写的一本深入讲解Python编程思想的经典著作。这本书通过实际案例,详尽地阐述了Python语言的核心概念、语法结构以及编程模式,旨在帮助读者掌握Python编程的精髓,并能运用...

    Complex Network Analysis in Python

    background in computer programming—namely, in Python programming. It expects from you no more than common sense knowledge of complex networks. The intention is to build up your CNA programming skills...

    Programming in Python 3(Python3程序开发指南(第二版))

    《Programming in Python 3》是Python 3编程领域的一本权威指南,主要针对Python 3.x版本进行深入讲解。本书的第二版旨在帮助初学者和有一定经验的开发者全面理解Python 3的核心概念、语法特性以及如何高效地进行...

    Programming in Python 3 Second Edition

    Programming in Python 3 A Complete Introduction to the Python Language Second Edition Mark Summerfield Addison-Wesley 2010 Pearson Education, Inc. Python is probably the easiest-to-learn and nicest-...

    Programming in Python 3 带源码

    《Programming in Python 3》是一本专为初学者和有一定经验的程序员设计的教材,英文版虽然对于非英语母语者可能稍有挑战,但内容却深入浅出,非常适合自学。这本书涵盖了Python 3编程语言的基础知识,以及如何有效...

    Think DSP Digital Signal Processing in Python epub

    Think DSP Digital Signal Processing in Python 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Unsupervised Machine Learning in Python [2016]

    LazyProgrammer, "Unsupervised Machine Learning in Python: Master Data Science and Machine Learning with Cluster Analysis, Gaussian Mixture Models, and Principal Components Analysis" 2016 | ASIN: B01...

    Programming in Python 3, 2nd Edition

    Programming in Python 3, Second Edition, brings together all the knowledge you need to write any program, use any standard or third-party Python 3 library, and create new library modules of your own....

    Udemy - Deep Learning Convolutional Neural Networks in Python

    Deep Learning: Convolutional Neural Networks in Python Computer Vision and Data Science and Machine Learning combined! In Theano and TensorFlow Created by Lazy Programmer Inc. Last updated 5/2017 ...

Global site tag (gtag.js) - Google Analytics