【问题标题】:"None" cannot be compared with numbers. Any other alternatives?“无”不能与数字相比较。还有其他选择吗?
【发布时间】:2020-06-20 03:20:49
【问题描述】:

在 Python 2 中,您可以将 None 与解决方案中的整数和浮点数进行比较,就像通过比较找到最小的数。但在 Python 3 中,它们无法进行比较。您在 Python 3 中是否有任何替代关键字或解决方案?

TypeError: '>' not supported between instances of 'int' and 'NoneType'

这是我的代码:

l = None
s = None

while True:
    n = input("Enter a number: ")
    if (n == "done") :
        break
    try:
        num = int(n)
    except:
        print ("Invalid input")
        continue
    if (s is None):
        s = num
    if (num > l) :
        l = num
    elif (num < s) :
        s = num

def done(l,s):
    print ("Maximum is", l)
    print ("Minimum is", s)

done(l,s)

【问题讨论】:

  • 取决于上下文。通常你只需在某处放一个value is[ not] None

标签: python python-3.x numbers int nonetype


【解决方案1】:

您可以使用条件(三级)运算符,即。而不是:

if x > y:

使用:

if (0 if x is None else x) > (0 if y is None else y):

【讨论】:

  • 简单一点:(x or 0) &gt; (y or 0).
  • 如果期望的行为是将None 视为 0,它会起作用,尽管这不是 Python 2 的行为。我想这取决于用例。
【解决方案2】:

在允许 None 类型的字段中,您可以先检查变量是否为 none 类型,如果不满足条件,则继续检查整数:

if x is not None:
    if x > y:
        # proceed with the operations  

【讨论】:

    【解决方案3】:

    由于None 在 Python 2 中有效地充当负无穷[*],您可以使用(代替x &lt; y

    False if y is None else True if x is None else x < y
    

    我们首先检查y,因此当xy 都是None 时,结果是False

    >>> def f(x, y):
    ...   return False if y is None else True if x is None else x < y
    ...
    >>> f(None, None)
    False
    >>> f(None, -10000)
    True
    >>> f(-10000, None)
    False
    

    如果您要定义一个函数,您应该使用if 语句来编写它,以便清楚起见:

    def f(x, y):
        if y is None:
            return False
        if x is None:
            return True
        return x < y
    

    [*] 更准确地说,None 充当&lt;= 隐含的格的底部。

    【讨论】:

    • 简单一点:y is not None and (x is None or x &lt; y).
    • 更短,但在这种情况下,我希望将涉及的不同运营商的数量保持在最低限度。
    【解决方案4】:

    您在比较之前过滤掉 None 值,看看这是否有帮助:

    >>> l = [None, 2,3,5, None, 10]
    >>> for i in l:
    ...       if i is not None:
    ...             print(i>5)
    ...       else:
    ...             print(None)
    ...
    None
    False
    False
    False
    None
    True
    

    加法

    l = None
    s = None
    
    def isNone(a):
        if a is not None:
            return True
        else:
            return False
    
    while True:
        n = input("Enter a number: ")
        if (n == "done") :
            break
        try:
            num = int(n)
        except:
            print ("Invalid input")
            continue
        if (s is None):
            s = num
        if isNone(l):
            if (num > l) :
                l = num
        elif (num < s) :
            s = num
    
    def done(l,s):
        print ("Maximum is", l)
        print ("Minimum is", s)
    
    done(l,s)
    

    由于代码的功能非常模糊,请看看这是否有帮助,您只是检查元素是否为None,如果不是,则它将用于条件检查。

    【讨论】:

    • 我添加了一些东西,请检查!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多