Python - 如果不是0.0的语句 [英] Python - If not statement with 0.0

查看:124
本文介绍了Python - 如果不是0.0的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于的问题,如果不是 Python 2.7 中的语句。

I have a question regarding if not statement in Python 2.7.

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

I have written some code and used if not statements. In one part of the code I wrote, I refer to a function which includes an if not statement to determine whether an optional keyword has been entered.

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

It works fine, except when 0.0 is the keyword's value. I understand this is because 0 is one of the things that is considered 'not'. My code is probably too long to post, but this is an analogous (albeit simplified) example:

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

但是,在这种情况下我还剩下:

However, in this case I got left with:

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

我希望得到的地方:

[1, 9, 0, 81]

在上面的例子中我可以使用列表理解,但假设我想使用该函数并获得所需的输出我怎么能这样做?

In the above example I could use a list comprehension, but assuming I wanted to use the function and get the desired output how could I do this?

我以为我曾经想过:

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

这有效,但看起来有点笨重的做法。如果有人有另一种方式会很好我会非常感激。

This works, but seems like a bit of a clunky way of doing it. If anyone has another way that would be nice I would be very appreciative.

推荐答案

问题



你理解得对。 not 0 (以及 not 0.0 )返回 True in 的Python 。可以进行简单测试,看看:

Problem

You understand it right. not 0 (and also not 0.0) returns True in Python. Simple test can be done to see this:

a = not 0
print(a)

Result: True

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

Thus, the problem is explained. This line:

if not x:

必须更改为其他内容。

有几种方法可以解决问题。我只是列出他们从我认为最好的解决方案到最后可能的解决方案:

There are couple of ways which can be done to fix the issue. I am just going to list them from what I think is the best solution down to the last possible solutions:



  1. 处理所有可能的有效案例

因为 square 自然期望输入一个数字,但不包括复数和应该返回否则,我认为最好的解决方案是使用进行评估,如果不是isinstance(x,numbers.Number)或isinstance(x,数字) .Complex):

Since square should naturally expect a number input with the exclusion of complex number and should return an error otherwise, I think the best solution is to evaluate using 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 指出这一点。)

numbers.Number is the abstract class to check if argument x is a number (credit to Copperfield for pointing this out).

摘自 Python标准库文档解释只是你需要什么 - 复数除外:

Excerpt from Python Standard Library Documentation explains just what you need - with the exception of complex number:


class numbers.Number

数字层次结构的。如果您只是想检查
参数x是否为数字,而不关心什么类型,请使用 isinstance(x,
数字)

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).

但是,您不希望输入为复数。所以,只需使用或isinstance(x,numbers.Complex)省略它

But, you don't want the input to be complex number. So, just omit it using or isinstance(x, numbers.Complex)


这样,你按照
的方式编写 square square 想要它。我认为,这个解决方案是凭借
全面性 最佳解决方案。

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 you have a list valid inpug data types you, you could also put up just those specific data types you want to handle. That is, you don't want to handle the cases for data types other than what you have specified. Examples:

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.

(上面的代码以更多 Pythonical 方式完成,如 cat

(Code above is done in more Pythonical way, as suggested by cat)



  1. 不处理不可能的案例:你知道用户的用处作为输入

  1. Not handling impossible cases: you know what the users would not put up as input.

如果你知道的话,可以更宽松地思考 - 不是你想要处理的数据类型,就像在第二个解决方案中那样 - 但是用户不能处理的数据类型 put,那么你可以像这样进行更宽松的条件检查:

Think it more loosely, if you know - not the data types you want to handle like in the second solution - but the data types which the user would not put, then you can have looser condition check like this:

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




我认为,这个解决方案是 最简单但功能强大的检查 一个
第三好

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

The only downside of this solution is that you don't handle complex type. Therefore can only be implementing by owing to the fact that the users would not have complex number as the input.



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

例如,如果你知道x总是 int - 因此唯一可能的输入错误是 - 然后我们可以简单地编写逻辑以避免 y x 时,正在评估

For example, if you know that x is always int or None - and thus the only possible input error is None - then we can simply write the logic to avoid y being evaluated only when x is None like this:

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.

...而 用于危险 你不知道完全用户将为输入提供什么。否则,这个解决方案很好,也是最简单的。

...and yet the most dangerous for being used if you do not know exactly what the users would put up for the input. Otherwise, this solution is fine and is also the simplest.

您的解决方案,我认为或多或少属于此类别。您知道用户将提供什么输入以及用户不会提供什么。因此,使用此解决方案或您自己的解决方案:

Your solution, I think more or less belongs to this category. You know what input the user will give and what the user will not. Thus, using this solution or your own solution:

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

很好,除了示例解决方案更简单

Is fine, except that the example solution is simpler






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

 [1, 9, 0, 81]

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

这篇关于Python - 如果不是0.0的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆