【问题标题】:Why does 'if not ' check empty string in python [duplicate]为什么'if not'检查python中的空字符串[重复]
【发布时间】:2019-12-02 04:10:20
【问题描述】:

您好,我有一个关于 py 中的“如果不是 ..”的快速问题。

这是一个应该接受字符串列表并返回字符串的函数。

我想知道第一行(如果不是 strs)是做什么的?我猜它会检查输入列表 strs 是否为空?它是如何工作的? 'if not' 不检查它是否与预期值相反,例如'if not None'? 谢谢!

def longestPrefix(strs):

        # Empty list
        if not strs: return ""
         ....

【问题讨论】:

标签: python python-3.x


【解决方案1】:

空字符串在布尔上下文中为 False,因此 if not FalseTrue

【讨论】:

  • 嗯,not [False]False
  • @StephenRauch 对不起,我将编辑它,我的意思是括号作为占位符,例如 if not False is True ,但我想表明 False 是空字符串
猜你喜欢
  • 2018-11-28
  • 2013-01-30
  • 2014-06-27
  • 1970-01-01
  • 2014-07-06
  • 2012-06-28
  • 2019-08-05
  • 1970-01-01
相关资源
最近更新 更多