最新消息:20210917 已从crifan.com换到crifan.org

【无需解决】Python中OrderedDict有值但是if判断却是False

Python crifan 448浏览 0评论
折腾:
【未解决】Win10中上线对接iOS自动抓包工具
期间,看到一个奇怪的现象:
    allAccountMarkerDict = findAllMarkerAccountInfo(dataLineList)
    if not allAccountMarkerDict:
        logging.error("Fail to parse out all account marker account from data file %s", dateFilePath)
        return
此时:allAccountMarkerDict不是None,而是有值的:
OrderedDict([('gh_25e1a8597a58/MokuGuitar', 'exception')])
not allAccountMarkerDict 却是True
很是奇怪。
python ordereddict check if none
Python: Checking if a ‘Dictionary’ is empty doesn’t seem to work – Stack Overflow
python – Pythonic way to check empty dictionary and empty values – Stack Overflow
python – Check that item exists in a dictionary of OrderedDict – Stack Overflow
Python | Check if dictionary is empty – GeeksforGeeks
去加上bool试试?
    allAccountMarkerDict = findAllMarkerAccountInfo(dataLineList)
    isAllMarkerNotEmpty = bool(allAccountMarkerDict)
    isAllMarkerEmpty = not isAllMarkerNotEmpty
    # if not allAccountMarkerDict:
    if isAllMarkerEmpty:
结果:
是可以的。
不过也突然发现:
之前好像就是本身OrderedDict是空的
所以才返回False
然后not False才是True,才导致输出错误信息的。
即:
之前代码:
    allAccountMarkerDict = findAllMarkerAccountInfo(dataLineList)
    if not allAccountMarkerDict:
逻辑本身是对的。没问题的。
所以是自己搞错了。
OrderedDict本身没问题。

后续OrderedDict有值,也就没报错了:


【总结】
此处代码:
    allAccountMarkerDict = findAllMarkerAccountInfo(dataLineList)
    if not allAccountMarkerDict:
        xxx
是没问题的。是自己的逻辑判断有误。代码没问题。

转载请注明:在路上 » 【无需解决】Python中OrderedDict有值但是if判断却是False

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.175 seconds, using 23.34MB memory