我想知道python是否有任何函数,例如php空函数(http://php.net/manual/en/function.empty.php),它检查变量是否为空并符合以下条件
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
另请参阅之前的答案,其中推荐了not
keyword
如何在Python中检查列表是否为空? https://stackoverflow.com/questions/1725517/how-to-check-if-a-list-is-empty-in-python
它概括为不仅仅是列表:
>>> a = ""
>>> not a
True
>>> a = []
>>> not a
True
>>> a = 0
>>> not a
True
>>> a = 0.0
>>> not a
True
>>> a = numpy.array([])
>>> not a
True
值得注意的是,它不适用于“0”作为字符串,因为该字符串实际上包含某些内容 - 包含“0”的字符。为此,您必须将其转换为 int:
>>> a = "0"
>>> not a
False
>>> a = '0'
>>> not int(a)
True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)