【问题标题】:Reduce if loops to a single line将 if 循环减少到一行
【发布时间】:2022-12-22 00:50:49
【问题描述】:

我想减少 Python 中的以下 if 循环。有什么有效的方法吗?

if a and b:
  print("case1")
elif not a and b:
  print("case2")
elif not a:
  print("case3")

【问题讨论】:

  • 这些不是 if 循环(不存在)。相反,它们是 if 语句。

标签: python python-3.x


【解决方案1】:
print("case1" if a and b else "case2" if not a and b else "case3")

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2018-08-28
    • 1970-01-01
    • 2011-12-22
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2021-04-23
    相关资源
    最近更新 更多