推荐学习书目
› 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
daiqiangbudainiu
V2EX  ›  Python

判断 Dict 是否是空,我这样写是不对的吗?

  •  
  •   daiqiangbudainiu · Feb 21, 2019 · 5512 views
    This topic created in 2822 days ago, the information mentioned may be changed or developed.

    如 debug 的截图

    QQ20190221-182258.png

    link_and_name 这个是一个 dict,可以看到他目前是空 {} 但是我的代码中

    if not link_and_name:
        return item
    

    并不管用,还是没有 return

    请问这是怎么回事


    抱歉抱歉,我看到了,类型不是 Dict,是我代码逻辑的错误,打扰了

    11 replies  •  2019-02-28 15:39:18 +08:00
    shm7
    shm7
    Feb 21, 2019 via iPhone
    1
    看看 stdlib 文档,对这些 collection bool value False 是指 len=0
    zhouyang1203
    zhouyang1203
    Feb 21, 2019 via Android❤️2
    2
    楼主看样子是在爬不得了的东西
    find456789
    find456789
    Feb 21, 2019❤️1
    3
    你的判断方法是正确的, 至于为何不如预期



    你 type (link_and_name) 就明白了

    点赞啊
    longbye0
    longbye0
    Feb 21, 2019 via Android
    4
    RBQ。RBQ。
    mayorbryant
    mayorbryant
    Feb 21, 2019
    5
    {str}'{}', 你所谓的是个字典其实是个字符串'{}',这个字符串非空,所以 not link_and_name is False
    另外 elif os.path.exists('info') is False: 可以直接写成 elif not os.path.exists('info'):
    baiye23333
    baiye23333
    Feb 21, 2019
    6
    判断一个 `dict` 是否为空,应该用 `len(dict.keys())==0`,你这样是判断是否为 `None`,判断是否在 dict 中,是 if key in dict.
    weyou
    weyou
    Feb 21, 2019 via Android
    7
    @baiye23333 刚从别的语言转过来吧
    iccfish
    iccfish
    Feb 21, 2019❤️2
    8
    New91Crawler ……(#`O ′)妖妖灵吗,这里有人蛇黄
    yake1015
    yake1015
    Feb 22, 2019
    9
    你那是个字符串,不是字典
    tcpdump
    tcpdump
    Feb 22, 2019
    10
    不得了, 爬 91
    wwg1994
    wwg1994
    Feb 28, 2019
    11
    等等 你在爬 91 ?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1629 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 00:07 · PVG 08:07 · LAX 16:07 · JFK 19:07
    ♥ Do have faith in what you're doing.