【问题标题】:python logical operator with variable number of arguments [duplicate]具有可变数量参数的python逻辑运算符[重复]
【发布时间】:2018-06-15 05:05:52
【问题描述】:

python 中有没有一种方法可以检查逻辑运算符 AND 与可变数量的参数。例如:

def and_function(a,b,c,d,e....):
    if a and b and c and d and c and d and e and . and . is True:
        return True

我看到使用 *args 函数和使用计数器是可能的。但想知道是否有更好的方法。

【问题讨论】:

标签: python args


【解决方案1】:

你基本上想要all(),但要自定义它你可以使用这个:

def and_function(*args):
    return all([a > 5 for a in args])

【讨论】:

  • 请注意,在any()all() 的情况下,最好不要先生成一个列表,然后将其作为可迭代对象传递,因为如果给定一个生成器,两者都会短路,但你的列表有完全生成。
猜你喜欢
  • 2019-11-07
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 2020-08-24
  • 2012-03-28
  • 2019-04-21
  • 2011-03-14
  • 2020-10-02
相关资源
最近更新 更多