fromiter()给出“int()参数必须是一个字符串”

时间:2017-10-10 21:27:59

标签: python numpy vectorization

我遇到了在数组上运行fromiter的问题:

>>>import numpy as np
>>>arr = np.array([0,0,0,0,0,0,0,0,0,0])
>>>brr = np.array([2, 4])
>>>def fnc(arr, b):
>>>    ar[br] += 2
>>>    ar[br-1] += 1
>>>    ar[br+1] += 1
>>>fnc(arr, brr[0])
>>>arr
array([0, 1, 2, 1, 0, 0, 0, 0, 0, 0])
>>>np.fromiter((fnc(arr, brrel) for brrel in brr), brr.dtype)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

只是好奇:为什么会发生这种情况?有任何想法吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

这是我的错误,没有必要进行矢量化,Python可以自己完成:

>>>import numpy as np; a = np.zeros(10); b = np.array([1,3,5]); a; b;
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
array([1, 3, 5])
>>>def fnc(arr, b):
>>>    ar[br] += 2
>>>    ar[br-1] += 1
>>>    ar[br+1] += 1
>>>
>>> fnc(a, b)
>>> a
array([ 1.,  2.,  2.,  2.,  2.,  2.,  1.,  0.,  0.,  0.])