我想在Numba类中使用字典,但是我不确定要为类型说明加上什么:
@numba.jitclass([("cnts", ???)])
class Counter:
def __init__(self):
self.cnts = numba.typed.Dict.empty(
key_type=numba.float64, value_type=numba.int64
)
有什么主意吗?
答案 0 :(得分:1)
这适用于Numba 0.45.1:
import numba
@numba.jitclass([("cnts", numba.types.DictType(numba.float64, numba.int64))])
class Counter:
def __init__(self):
self.cnts = numba.typed.Dict.empty(
key_type=numba.float64, value_type=numba.int64
)
def add(self, x):
if x not in self.cnts:
self.cnts[x] = 0
self.cnts[x] += 1
c = Counter()
c.add(1.1)
c.add(1.1)
c.add(0.5)
print(c.cnts) # {1.1: 2, 0.5: 1}