这篇文章主要介绍了python使用a来源gao*daima.com搞@代#码网ny判断一个对象是否为空的方法,并给出了改进的方法供大家对比参考,具有一定的借鉴价值,需要的朋友可以参考下
本文实例讲述了python使用any判断一个对象是否为空的方法。分享给大家供大家参考。
具体实现代码如下:
代码如下:
>>> eth = {“eth0″:”192.168.1.1″}
>>> any(eth)
True
>>> eth = {}
>>> any(eth)
False
>>> any(eth)
True
>>> eth = {}
>>> any(eth)
False
判断list是否为空
传统的方式:
代码如下:
if len(mylist):
# Do something with my list
else:
# The list is empty
# Do something with my list
else:
# The list is empty
由于一个空 list 本身等同于 False,所以可以直接:
代码如下:
if mylist:
# Do something with my list
else:
# The list is empty
# Do something with my list
else:
# The list is empty
希望本文所述对大家的Python程序设计有所帮助。
以上就是python使用any判断一个对象是否为空的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!