【发布时间】:2023-03-29 14:43:01
【问题描述】:
我正在尝试检查一个数字是否在 int8s 的 NumPy 数组中。我试过这个,但它不起作用。
from numba import njit
import numpy as np
@njit
def c(b):
return 9 in b
a = np.array((9, 10, 11), 'int8')
print(c(a))
我得到的错误是
Invalid use of Function(<built-in function contains>) with argument(s) of type(s): (array(int8, 1d, C), Literal[int](9))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of intrinsic-call at .\emptyList.py (6)
如何在保持性能的同时解决此问题? 将检查数组的两个值,1 和 -1,长度为 32 项。它们没有排序。
【问题讨论】:
-
Numba 似乎不支持 in 用于 numpy-arrays,但它支持用于集合。所以你唯一要做的就是把 b 改成 set(b)
-
@max9111 如果我理解正确,那不会需要一点时间吗?我会多次这样做,所以速度很重要。
-
最快的算法取决于数据。 (数组是否已排序?如果它在数组中,是否要检查多个值?)
-
@max9111 数组未排序,我正在检查两个值,1 和 -1。该数组将有 32 个项目长。我会添加这个。