【发布时间】:2019-08-15 18:20:13
【问题描述】:
我有以下代码尝试将键值对存储到 numba 字典中。 Numba 的官方页面说新的类型化字典支持数组作为键,但我无法让它工作。 错误消息说密钥不能是散列。 知道如何让它工作吗?
In [7]: from numba.typed import Dict
...: from numba import types
...: import numpy as np
In [15]: dd = Dict.empty(key_type=types.int32[::1], value_type=types.int32[::1],)
In [16]: key = np.asarray([1,2,3], dtype=np.int32)
In [17]: dd[key] = key
错误信息:
TypingError:在 nopython 模式管道中失败(步骤:nopython 前端) array(int32, 1d, C) 类型的未知属性“hash”
编辑: 我可能错过了一些东西。我可以在解释器中使用 types.UniTuple(没有 @jit 装饰器)。但是,当我将以下函数放入脚本 a.py 并使用命令“python a.py”运行它时,出现 UniTuple not found 错误。
@jit(nopython=True)
def go_fast2(date, starttime, id, tt, result): # Function is compiled and runs in machine code
prev_record = Dict.empty(key_type=types.UniTuple(types.int64, 2), value_type=types.UniTuple(types.int64, 3),)
for i in range(1, length):
key = np.asarray([date[i], id[i]], dtype=np.int64)
thistt = tt[i]
thistime = starttime[i]
if key in prev_record:
prev_time = prev_record[key][0]
prev_tt = prev_record[key][1]
prev_res = prev_record[key][2]
if thistt == prev_tt and thistime - prev_time <= 30 * 1000 * 1000: # with in a 10 seconds window
result[i] = prev_res + 1
else:
result[i] = 0
prev_record[key] = np.asarray((thistime, thistt, result[i]), dtype=np.int64)
else:
result[i] = 0
prev_record[key] = np.asarray((thistime, thistt, result[i]), dtype=np.int64)
return
【问题讨论】:
标签: numba