使用多个列表时,如何避免在零时避免错误
我有2个列表,每个列表中有4个值。每次运行代码时,这些值都会更改。
probability = [4, 0.004e-2, 5.5, 0.0]
miss = [6954, 12507, 3621, 10440]
我一直在将列表插入此公式中,该公式在看到0时会破裂。我该怎么做才能停止错误(用空或0替换)?
severity = [a * 100 / b for a, b in zip(miss, probability)]
I have 2 lists with 4 values in each. These values will change every time the code is run.
probability = [4, 0.004e-2, 5.5, 0.0]
miss = [6954, 12507, 3621, 10440]
I have been plugging the lists into this formula which breaks when it sees a 0. What can I do to stop the errors (either replaced with a null or 0)?
severity = [a * 100 / b for a, b in zip(miss, probability)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也就是说,当
b
为0时,请使用条件表达式避免进行除法。That is, use a conditional expression to avoid doing division when
b
is 0.一种解决方案是测试
b
的值永远不会为零。如果是这样,请提出异常,或者分配严重性
您指出的一些非平凡的值,例如none
。您可以做这样的事情:One solution is to test that values of
b
are never zero. If it is, either raise an exception, or assignseverity
some non-trivial value such asNone
, as you pointed out. You could do something like this: