The*_*oro 19 python if-statement control-structure python-2.7
我有一个关于if not
声明的问题Python 2.7
.
我写了一些代码和使用过的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
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,我留下了:
you have not entered x
[1, 9, None, 81]
Run Code Online (Sandbox Code Playgroud)
在哪里我想得到:
[1, 9, 0, 81]
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我可以使用列表推导,但假设我想使用该函数并获得所需的输出,我该怎么做?
我有一个想法是:
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
Run Code Online (Sandbox Code Playgroud)
这有效,但似乎有点笨重的做法.如果有人有另一种方式会很好,我会非常感激.
Ian*_*Ian 18
你理解得对.not 0
(也not 0.0
)返回True
在Python
.可以通过简单的测试来看到:
a = not 0
print(a)
Result: True
Run Code Online (Sandbox Code Playgroud)
因此,解释了该问题.这一行:
if not x:
Run Code Online (Sandbox Code Playgroud)
必须更改为其他内容.
有几种方法可以解决问题.我只是将它们列为我认为最好的解决方案,直到最后可能的解决方案:
处理所有可能的有效案件.
既然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)
Run Code Online (Sandbox Code Playgroud)
numbers.Number是一个抽象类,用于检查参数x
是否为数字(归功于Copperfield指出这一点).
摘自Python标准库文档解释了刚才你所需要的-用复杂的数除外:
班级 号码.数量
数字层次结构的根.如果你只是想检查参数x是否是一个数字,而不关心什么类型,请使用isinstance(x,Number).
但是,您不希望输入是复数.所以,只是省略它or isinstance(x, numbers.Complex)
这样,你就
definition
可以square
按照你想要的方式编写.我认为,这种解决方案凭借其全面性是最佳解决方案.
仅处理要处理的数据类型.
如果您有一个列表有效的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
Run Code Online (Sandbox Code Playgroud)
对于您要包含或排除的不同数据类型,您可以根据需要轻松更改/扩展上述条件.我认为,这种解决方案凭借 其有效性检查的定制功能,是第二好的.
(上面的代码以python的方式完成,如cat所示)
不处理不可能的情况:你知道用户不会提出什么作为输入.
如果你知道 - 不是你想要在第二个解决方案中处理的数据类型 - 而是用户不会放置的数据类型,那么你可以更宽松地考虑它,那么你可以像这样检查更宽松的条件:
if not isinstance(x, numbers.Number): # this is ok, because the user would not put up complex number
Run Code Online (Sandbox Code Playgroud)
该解决方案,我认为,是第三个最好由由于是最简单但功能强大的检查之一.
此解决方案的唯一缺点是您不处理复杂类型.因此,只能通过用户不具有复数作为输入的事实来实现.
仅处理可能导致错误的已知可能输入的输入错误.
例如,如果您知道x总是int
或None
- 因此唯一可能的输入错误是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)
Run Code Online (Sandbox Code Playgroud)
该解决方案具有最简单的优点.
......,但最危险的使用,如果你不知道究竟是什么用户会拿出来输入.否则,这个解决方案很好,也是最简单的.
您的解决方案,我认为或多或少属于此类别.您知道用户将提供什么输入以及用户不会输入什么.因此,使用此解决方案或您自己的解决方案:
if not x and not str(x).isdigit():
Run Code Online (Sandbox Code Playgroud)
很好,除了示例解决方案更简单
根据您的情况,您可以使用上述任何解决方案来获得:
[1, 9, 0, 81]
Run Code Online (Sandbox Code Playgroud)
(旁注:为了便于阅读,我尝试将解决方案格式化为"规范解决方案".这样,那些有相同问题并且将来访问此页面的人可能能够找到更全面的解决方案可读)
由于您None
用来表示"此参数未设置",因此您应该使用is
关键字检查以下内容:
def square(x=None):
if x is None:
print "you have not entered x"
else:
y=x**2
return y
Run Code Online (Sandbox Code Playgroud)
检查类型很麻烦且容易出错,因为您必须检查所有可能的输入类型,而不仅仅是int
.
归档时间: |
|
查看次数: |
4559 次 |
最近记录: |