V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-08-08 13:49:29 +08:00 · 1303 次点击
    这是一个创建于 1695 天前的主题,其中的信息可能已经有所发展或是发生改变。

    《 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后面的变量没有返回给linepreclines迭代,搞不懂为什么,是我理解错了吗?

    chenstack
        1
    chenstack  
       2019-08-08 14:50:23 +08:00
    贴出的代码有两处拼写错误
    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
       2019-08-08 15:43:25 +08:00
    我的错...代码贴错了

    直接 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
       2019-08-08 16:36:08 +08:00
    @chenstack 我的错...代码贴错了,之前运行的时候变量名是对的。不过我把 txt 的内容换成老哥你的,能出结果。谢谢老哥了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3248 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:50 · PVG 18:50 · LAX 03:50 · JFK 06:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.