空列表等于无? [英] Empty list is equal to None or not?

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

问题描述

可能重复:
为什么"[] == False"评估为False什么时候如果不是[]"成功?

Possible Duplicate:
Why does "[] == False" evaluate to False when "if not []" succeeds?

根据python的三元运算符,我是python的新手

I am new to python as per ternary operator of python

>>> 'true' if True else 'false'  true
   true

我期望下面的代码输出为[],因为[]不等于None

i am expecting for below code output as [] because [] not equal to None

>>> a=[]
>>> a==None
False
>>> a if a else None
None

如果我错了,请纠正

谢谢 血气

推荐答案

空白列表[] 等于None.

但是,它可以求值为False-也就是说,其真实性"值为False. (请参阅OP上的注释中的源.)

However, it can evaluate to False--that is to say, its "truthiness" value is False. (See the sources in the comments left on the OP.)

因此,

>>> [] == False
False
>>> if []:
...     print "true!"
... else:
...     print "false!"
false!

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

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