3

我想按元素划分两个一维数组。会有除零,但我仍然想做除法。div/0 的结果无关紧要,稍后我会处理。

为什么会出现这个错误?为什么在我检查了我的数组的值后它会停止发生?运行代码时如何避免此错误?

>>> a=numpy.array([1, 2, 3, 0, 0])
>>> b=numpy.array([5, 3, 1, 0, 0])
>>> a/b
Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    a/b
  File "C:\Program Files (x86)\Python33\lib\idlelib\PyShell.py", line 60, in idle_showwarning
    file.write(warnings.formatwarning(message, category, filename,
AttributeError: 'NoneType' object has no attribute 'write'
>>> a
array([1, 2, 3, 0, 0])
>>> b
array([5, 3, 1, 0, 0])
>>> a/b
array([ 0.2       ,  0.66666667,  3.        ,         nan,         nan])
4

0 回答 0