2012-06-13 195 views
5

可能重复:
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':[]}) 

我编辑的这一点,因为我需要更多的帮助。你如何检查c是否为空? c简直等于[]

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

+0

如何定义“空白”?只是一个没有元素的列表? –

+0

我(和其他人)在回答您的上一个问题时实际使用了'if c'。仔细研究这些答案,你可能会学到一些东西。 –

+0

试图让你说的长度是正确的想法,应该有效。 – Levon

回答

3

在python中,空列表的计算结果为False。

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

不起作用: del theDict [键]仍然打印出{'a':['1','2'],'c':[],'b':['3',' “]} –