zoukankan      html  css  js  c++  java
  • 列表和生成器表达式

    1 协程函数的应用

      写一个装饰器用于让协程函数不需要输入再执行一次next()函数

      分析: 在装饰器中生成该协程函数的生成器, 并且执行一次next()函数

      具体描述如下

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    __author__ = 'weihuchao'
    
    def firstNext(func):
        def wrapper(*args, **kwargs):
            g = func(*args, **kwargs)
            next(g)
            return g
        return wrapper
    
    @firstNext
    def eater(name):
        print("{} start to eat".format(name))
        food_list = []
        while True:
            food = yield food_list
            print("{} eat {}".format(name, food))
            food_list.append(food)
    
    g = eater("egon")
    print(g.send("包子"))
    print(g.send("炸酱面"))
    

    2 面向过程编程

      实现 grep -rl 'python' c:/test 的功能

      一个函数实现

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    __author__ = 'weihuchao'
    
    import os
    
    def search(dir_name, partten):
        g = os.walk(dir_name)
        res = {"",}
        for i in g:
            for j in i[-1]:
                file_path = i[0] + "\" +j
                with open(file_path) as f:
                    for line in f:
                        if partten in line:
                            res.add(file_path)
        for line in res:
            if line:
                print(line)
    
    search("c:\test", "python")
    

      利用生成器来处理

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    __author__ = 'weihuchao'
    
    import os
    
    def firstNext(func):
        def wrapper(*args, **kwargs):
            g = func(*args, **kwargs)
            next(g)
            return g
        return wrapper
    
    @firstNext
    def search(target):
        while True:
            dir_name = yield
            g = os.walk(dir_name)
            for i in g:
                for j in i[-1]:
                    file_path = i[0] + "\" +j
                    target.send(file_path)
    
    @firstNext
    def opener(target):
        while True:
            file_path = yield
            with open(file_path) as f:
                target.send((file_path, f))
    
    @firstNext
    def cat(target):
        while True:
            file_path, f = yield
            for line in f:
                target.send((file_path, line))
    
    @firstNext
    def grep(partten, target):
        while True:
            file_path, line = yield
            if partten in line:
                target.send(file_path)
    
    @firstNext
    def printer():
        while True:
            file_path = yield
            print(file_path)
    
    g = search(opener(cat(grep("python",printer()))))
    g.send("c:\test")
    

    3 列表生成式, 生成器表达式

      列表生成式的具体形式

    列表 = [处理变量形成列表元素 for循环 if判断 for循环2]  

      等同于

    for循环
        if判断    
            for循环
                元素=表达式
        列表.append(元素)

      生成器表达式是 列表生成式的中括号编程圆括号

      其中获得的是一个生成器

      可以通过 list(获得的生成器) 来快速生成一个列表

    4 声明式编程

      声明式编程主要是注重于定义, 不具体去了解实现

      Python中有很多封装好的方法, 如reduce和map等

    人若有恒 无所不成
  • 相关阅读:
    POJ 2752 Seek the Name, Seek the Fame
    POJ 2406 Power Strings
    KMP 算法总结
    SGU 275 To xor or not to xor
    hihocoder 1196 高斯消元.二
    hihoCoder 1195 高斯消元.一
    UvaLive 5026 Building Roads
    HDU 2196 computer
    Notions of Flow Networks and Flows
    C/C++代码中的笔误
  • 原文地址:https://www.cnblogs.com/weihuchao/p/6699240.html
Copyright © 2011-2022 走看看