python使用any判断一个对象是否为空的方法

yipeiwu_com6年前Python基础

本文实例讲述了python使用any判断一个对象是否为空的方法。分享给大家供大家参考。

具体实现代码如下:

复制代码 代码如下:
>>> eth = {"eth0″:"192.168.1.1″}
>>> any(eth)
True
>>> eth = {}
>>> any(eth)
False

判断list是否为空

传统的方式:

复制代码 代码如下:
if len(mylist):
    # Do something with my list
else:
    # The list is empty

由于一个空 list 本身等同于 False,所以可以直接:
复制代码 代码如下:
if mylist:
    # Do something with my list
else:
    # The list is empty

希望本文所述对大家的Python程序设计有所帮助。

相关文章

PyTorch实现更新部分网络,其他不更新

torch.Tensor.detach()的使用 detach()的官方说明如下: Returns a new Tensor, detached from the current gra...

Python 获取中文字拼音首个字母的方法

Python 获取中文字拼音首个字母的方法

Python:3.5 代码如下: def single_get_first(unicode1): str1 = unicode1.encode('gbk') try: ord...

使用Python进行稳定可靠的文件操作详解

考虑下述Python代码片段。对文件中的数据进行某些操作,然后将结果保存回文件中: 复制代码 代码如下:with open(filename) as f:   inp...

总结python实现父类调用两种方法的不同

总结python实现父类调用两种方法的不同

python中有两种方法可以调用父类的方法: super(Child, self).method(args)  Parent.method(self, args) 我用其中的一...

django中forms组件的使用与注意

forms组件 django框架提供了一个Form类,来进行web开发中的表单提交数据的处理工作。 导入相关模块 from django import forms from dja...