你如何检查,如果列表为空? [英] How do you check if list is blank?

查看:103
本文介绍了你如何检查,如果列表为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/53513/python-what-is-the-best-way-to-check-if-a-list-is-empty\">Python:什么是检查一个列表为空最好的方法是什么?


 高清CleanWhiteSpace(theDict):
    东西= []    关键在theDict.items()值:
        在值D:
            如果value =!
                stuff.append(D)
                打印ð
                theDict [关键] =东西
            如果不珍惜[D]:
                打印值
        东西= []
    返回theDict
    打印CleanWhiteSpace({'一':['1','2'],'B':['3',''],'C':[]})

我编辑这一点,因为我需要更多的帮助。如何判断检查的C 是空白的?为 C 就等于 []

我试过 == [] []和获得的长度和 ==,但似乎没有任何工作。


解决方案

在Python中,空单计算为False。

 如果不是C:
   打印列表为空
其他:
   打印这份名单是不是空的

Possible Duplicate:
Python: What is the best way to check if a list is empty?

def CleanWhiteSpace(theDict):
    stuff=[]

    for key,value in theDict.items():
        for d in value:
            if value != " ":
                stuff.append(d)    
                print d
                theDict[key]=stuff
            if not value[d]:
                print value
        stuff=[]
    return theDict
    print CleanWhiteSpace({'a':['1','2'],'b':['3',' '],'c':[]})

I edited this because I need more help. How do you check if c is blank? Is c is simply equal to []?

I've tried ==[] and "[]" and getting the length and == "", but nothing seems to work.

解决方案

In python, an empty list evaluates to False.

if not c:
   print "The list is empty"
else:
   print "The list is not empty"

这篇关于你如何检查,如果列表为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆