Python list 判断是否为空的几种方法

Python 2015-12-09 00:34 暂无评论

我比较习惯用if a == []

  1. 判断内容
a = []
if a == []:
    print "a is empty!"
else:
    print "a not empty!"
  1. bool值判断
a = []
if bool(a) == False:
    print "a is empty!"
else:
    print "a not empty!"
  1. if not
a = []
if not a:
    print "a is empty!"
else:
    print "a not empty!"
  1. len内容
a = []
if len(a) == 0:
    print "a is empty!"
else:
    print "a not empty!"
  1. 应该也是True判断吧 -.-
a = []
if a:
    print "a not empty!"
else:
    print "a is empty!"
暂无评论