zoukankan      html  css  js  c++  java
  • Day 13 生成器,函数补充

    day13思维导图

    一.三元表达式 Ternary operator

    Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows to test a condition in a single line replacing the multiline if-else making the code compact.

    [on_true] if [expression] else [on_false] 

    二.生成式

    列表生成式

    [x for x in data if condition]
    [exp1 if condition else exp2 for x in data]

    字典生成式

    #d = {key: value for (key, value) in iterable}
    d1 = {‘x‘: 1, ‘y‘: 2, ‘z‘: 3}
    d2 = {k: v for (k, v) in d1.items()}
    print(d2)

    集合生成式

    s1={x for x in range(10)}
    print(s1)

     

    三.生成器 Generator

    Generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.

    Example 1: Reading Large Files

    csv_gen = (row for row in open(file_name))

    This is a more succinct way to create the list csv_gen. You’ll learn more about the Python yield statement soon. For now, just remember this key difference:

    • Using yield will result in a generator object.

    • Using return will result in the first line of the file only.

    Example 2: Generating an Infinite Sequence

    >>> gen = infinite_sequence()
    >>> next(gen)
    0
    >>> next(gen)
    1
    >>> next(gen)
    2
    >>> next(gen)
    3

    Here, you have a generator called gen, which you manually iterate over by repeatedly calling next(). This works as a great sanity check to make sure your generators are producing the output you expect.

    四.函数的递归调用 Recursive functions

    What is recursion?

    Recursion is the process of defining something in terms of itself.

    A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

    Python Recursive Function

    In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions.

    The following image shows the working of a recursive function called recurse

    递归

     

    Advantages of Recursion

    Recursive functions make the code look clean and elegant. A complex task can be broken down into simpler sub-problems using recursion. Sequence generation is easier with recursion than using some nested iteration.

    Disadvantages of Recursion

    Sometimes the logic behind recursion is hard to follow through. Recursive calls are expensive (inefficient) as they take up a lot of memory and time. Recursive functions are hard to debug.

    五 匿名函数 anonymous function

    A lambda function is a small anonymous function.

    A lambda function can take any number of arguments, but can only have one expression.

    Syntax

    lambda arguments : expression

    Example:

    x = lambda a, b, c: a + b + c
    print(x(5, 6, 2))

    Why Use Lambda Functions?

    Why Use Lambda Functions? The power of lambda is better shown when you use them as an anonymous function inside another function.

    def myfunc(n):
    return lambda a : a * n

    mydoubler = myfunc(5)

    print(mydoubler(333))

    Use lambda functions when an anonymous function is required for a short period of time.

    六 面向过程的编程

    POP 面向过程

    POP, refers to Procedural Oriented Programming and its deals with programs and functions. Programs are divided into functions and data is global.

    OOP 面向对象

    OOP, refers to Object Oriented Programming and its deals with objects and their properties. Major concepts of OOPs are −

    • Class/objects 类/对象

    • Abstraction 抽象

    • Encapsulation /ɪnˌkæpsjuˈleɪʃn/ 封装

    • Polymorphism /,pɒlɪ'mɔːfɪzəm/ 多态

    • Inheritance 继承

  • 相关阅读:
    解决hadoop中 bin/hadoop fs -ls ls: `.': No such file or directory问题
    ERROR namenode.NameNode: Failed to start namenode. java.lang.IllegalArgument
    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Users/lonecloud/tomcat/apache-tomcat-7.0.70 2/webapps/myproject/WEB-INF/classes/cn/lone
    创建Maven web工程不能解析EL表达式的解决办法
    mac中的myeclipse的控制台中文乱码问题解决办法
    Java采用内部构造器Builder模式进行对类进行构建
    java定时器的使用(Timer)
    传统的线程技术
    线程的理解
    Ibatis学习总结7--SqlMapClient 执行 SQL 语句
  • 原文地址:https://www.cnblogs.com/fengshili666/p/14219343.html
Copyright © 2011-2022 走看看