gpt4 book ai didi

Python - 如果不是 0.0 的语句

转载 作者:IT老高 更新时间:2023-10-28 20:52:57 28 4
gpt4 key购买 nike

我对 Python 2.7 中的 if not 语句有疑问。

我已经编写了一些代码并使用了 if not 语句。在我编写的代码的一部分中,我引用了一个函数,该函数包含一个 if not 语句来确定是否输入了可选关键字。

它工作正常,除非 0.0 是关键字的值。我理解这是因为 0 是被认为是“不”的事物之一。我的代码可能太长,无法发布,但这是一个类似的(尽管是简化的)示例:

def square(x=None):
if not x:
print "you have not entered x"
else:
y=x**2
return y

list=[1, 3, 0 ,9]
output=[]


for item in list:
y=square(item)
output.append(y)

print output

但是,在这种情况下,我得到了:

you have not entered x
[1, 9, None, 81]

我想去哪里:

[1, 9, 0, 81]

在上面的示例中,我可以使用列表推导式,但假设我想使用该函数并获得所需的输出,我该怎么做?

我的一个想法是:

def square(x=None):
if not x and not str(x).isdigit():
print "you have not entered x"
else:
y=x**2
return y

list=[1, 3, 0 ,9]
output=[]


for item in list:
y=square(item)
output.append(y)

print output

这可行,但似乎有点笨拙。如果有人有另一种很好的方式,我将非常感激。

最佳答案

问题

你没看错。 not 0 (还有 not 0.0 )返回 TruePython .可以做个简单的测试看看:

a = not 0
print(a)

Result: True

因此,问题得到了解释。这一行:

if not x:

必须改成别的东西。


解决方案

有几种方法可以解决这个问题。我将列出它们,从我认为最好的解决方案到最后可能的解决方案:


  1. 处理所有可能的有效情况

    自从 square应该自然期望一个排除复数的数字输入并且应该return否则会出错,我认为最好的解决方案是使用 if not isinstance(x, numbers.Number) or isinstance(x, numbers.Complex): 进行评估

    def square(x=None):
    if not isinstance(x, numbers.Number) or isinstance(x, numbers.Complex): # this sums up every number type, with the exclusion of complex number
    print ("you have not entered x")
    else:
    y=x**2
    return y

    list=[1, 3, 0 ,9]
    output=[]

    for item in list:
    y=square(item)
    output.append(y)

    print (output)

    numbers.Number 抽象类来检查参数 x是一个数字(感谢 Copperfield 指出这一点)。

    摘自Python Standard Library Documentation解释只是你需要什么 - 除了复数:

    class numbers.Number

    The root of the numeric hierarchy. If you just want to check if an argument x is a number, without caring what kind, use isinstance(x, Number).

    但是,您不希望输入是复数。因此,只需使用 or isinstance(x, numbers.Complex) 将其省略即可

    This way, you write the definition of square exactly the way you want it. This solution, I think, is the best solution by the virtue of its comprehensiveness.


  1. 只处理您想要处理的数据类型

    如果你有一个有效的 inpug 数据类型列表,你也可以只列出你想要处理的那些特定数据类型。也就是说,您不想处理您指定的数据类型以外的情况。例子:

    if not instance(x, int): #just handle int
    if not instance(x, (int, float)): #just handle int and float
    if not instance(x, (numbers.Integral, numbers.Rational)): #just handle integral and rational, not real or complex

    You may change/extend the condition above easily for different data types that you want to include or to excluded - according to your need. This solution, I think, is the second best by the virtue of its customization for its validity checking.

    (如 cat 所建议的那样,上面的代码以更 Pythonical 的方式完成)


  1. 不处理不可能的情况:您知道用户会输入什么内容

    如果您知道 - 不是像第二种解决方案那样要处理的数据类型 - 而是用户将 放入的数据类型,那么可以更宽松地考虑一下,那么您可以有更宽松的条件像这样检查:

    if not isinstance(x, numbers.Number): # this is ok, because the user would not put up complex number

    This solution, I think, is the third best by the virtue of being one of the simplest yet powerful checking.

    此解决方案的唯一缺点是您不处理复杂类型。因此,由于用户没有复数作为输入,因此只能通过实现。


  1. 仅处理可能导致错误的已知输入错误

    例如,如果您知道 x 总是 intNone - 因此唯一可能的输入错误是None - 然后我们可以简单地编写逻辑来避免y x 时被评估是 None像这样:

    def square(x=None):
    if x is None:
    print ("you have not entered x")
    else:
    y=x**2
    return y

    list=[1, 3, 0 ,9]
    output=[]

    for item in list:
    y=square(item)
    output.append(y)

    print (output)

    This solution has the virtue of being the simplest.

    ...然而最危险被使用如果你不知道确切用户会为输入提供什么.否则,这个解决方案很好,也是最简单的。

    你的解决方案,我觉得或多或少属于这一类。你知道用户会给出什么输入,用户不会给出什么。因此,使用此解决方案或您自己的解决方案:

    if not x and not str(x).isdigit():

    没问题,只是示例解决方案更简单


根据您的情况,您可以使用上述任何解决方案来获得:

 [1, 9, 0, 81]

(旁注:为了便于阅读,我尝试将解决方案格式化为“规范解决方案”。这样,那些有相同问题并在未来访问此页面的人可能能够找到更全面、更易读的解决方案)

关于Python - 如果不是 0.0 的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35572698/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com