当前位置 博文首页 > 文章内容

    【Python】关于如何判断一个list是否为空的思考

    作者: 栏目:未分类 时间:2020-07-14 9:00:48

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    前言

    今天随手翻 stackoverflow,看到问题叫 How do I check if a list is empty? 一看这个问题,不难猜到到这是一个刚学 Python 的人提问的,因为这个问题实在是太基础了,那么如何判断呢?

    写法

    写法一

    a = []
    if len(a) == 0:
        print("empty list")    
    

    写法二

    a = []
    if not a:
        print("empty list")
    

    这两种写法都很常见。那么问题来了,第一种写法用列表长度去判断列表为空可以理解,那么第二种写法直接去判断,这是怎么判断出来的呢?

    if

    为了解决这个问题,我们首先需要重新认识一下 if。关于 if 是用来做条件执行的这一点,大家肯定都知道。

    这里我们顺便看一下官方文档的描述

    The if statement is used for conditional execution:

    if_stmt ::=  "if" expression ":" suite
                 ("elif" expression ":" suite)*
                 ["else" ":" suite]
    

    It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true; then that suite is executed

    注意上面加粗的地方 true, 很明显 if 后面表达式预期值应该为 true 或者 false,也就是说写法二中的 a 最后的值也应该为 true 或者 false,那是怎么做到的呢?

    bool

    最简单的办法去判断 a 是否为 true 或者 false,就是使用内置函数 bool()

    >>> a = []
    >>> bool(a)
    False
    

    正常调用 bool() 这个函数的时候,会去调用变量的内建方法 __bool__(),那如何看一个变量有哪些内建方法呢?在上一篇__name__是什么提到了一个的函数 dir(),在这里也同样可以用来使用

    >>> dir(a)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    

    似乎没有看到 __bool__() 这个函数,那怎么办呢? 不用着急,官方文档中还说了这样的一句话

    When this method (__bool__()) is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true.

    当一个变量没有定义 __bool__ 的时候,不用着急,如果定义了 __len() 这个函数也是可以的,当长度不为 0 的时候则为 true

    总结

    之所以在写法二中可以用 if 直接判断列表 a 是否为空,是因为对于 list 来说,它没有内建方法 __bool__(),而有内建方法 __len__(),最后通过判断 list 长度是否为 0 来得出 true 或者 false 的,一旦为空,则判断结果为 false