推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
scp404
V2EX  ›  Python

小白求助,在看《python3-cookbook》遇到的问题

  •  
  •   scp404 · Aug 8, 2019 · 1973 views
    This topic created in 2610 days ago, the information mentioned may be changed or developed.

    《 python3-cookbook 》第一章第三节:保留最后 N 个元素

    from collections import deque
    
    def search(lines, pattern, history=5):
        previous_lines = deque(maxlen=history)
        for line in lines:
            if pattern in line:
                yield line, precious_lines
            previous_lines.append(line)
    
    # Example use on a file
    if __name__ == '__main__':
        with open(r'../../somefile.txt') as f:
            for line, preclines in search(f, 'python', 5):
                for pline in prevlines:
                    print(pline, end='')
                    print(line, end='')
                    print('-' * 20)
    

    代码运行没报错,但是为什么没有 print 出东西? 我调试了一下,好像是search函数里yield后面的变量没有返回给line和preclines迭代,搞不懂为什么,是我理解错了吗?

    chenstack
        1
    chenstack  
       Aug 8, 2019
    贴出的代码有两处拼写错误
    precious_lines -> previous_lines
    preclines ->prevlines

    改完后运行是 ok 的

    somefile.txt:
    java
    python3
    rust
    php
    js
    c++
    c
    go
    python3.7
    ruby

    output:
    java
    python3
    --------------------
    php
    python3.7
    --------------------
    js
    python3.7
    --------------------
    c++
    python3.7
    --------------------
    c
    python3.7
    --------------------
    go
    python3.7
    --------------------
    scp404
        2
    scp404  
    OP
       Aug 8, 2019
    我的错...代码贴错了

    直接 copy 书里的源码是这样的

    ```
    from collections import deque


    def search(lines, pattern, history=5):
    previous_lines = deque(maxlen=history)
    for line in lines:
    if pattern in line:
    yield line, previous_lines
    previous_lines.append(line)

    # Example use on a file
    if __name__ == '__main__':
    with open(r'../../cookbook/somefile.txt') as f:
    for line, prevlines in search(f, 'python', 5):
    for pline in prevlines:
    print(pline, end='')
    print(line, end='')
    print('-' * 20)
    ```

    没打印出东西

    我是不是回复不了图片。。。
    scp404
        3
    scp404  
    OP
       Aug 8, 2019
    @chenstack 我的错...代码贴错了,之前运行的时候变量名是对的。不过我把 txt 的内容换成老哥你的,能出结果。谢谢老哥了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4728 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 06:57 · PVG 14:57 · LAX 23:57 · JFK 02:57
    ♥ Do have faith in what you're doing.