创建一个 numba 类型的列表而不循环 python 列表

Create a numba typed List without looping over a python list

我想使用 numba.typed.List(将其称为 List)传递到包含在 njit 中的函数中。然而,这个 List 应该从现有的 python 列表中创建。

当我查看 documentation 时,您创建 List 的方式似乎是对其进行初始化,然后向其添加元素。但是,这需要您遍历 python 中已经存在的列表,这对于大型列表来说似乎效率低下。

例如:

from numba.typed import List
numba_list = List()
py_list = ["a", "b", "c"]
for e in py_list:
    numba_list.append(e)

In [17]: numba_list[0]
Out[17]: 'a'

Is there a way to set a List to the values of a python list without explicitly looping over the python list ?

我正在使用 numba.__version__ = '0.47.0'

您可以通过以下方式对类型列表使用列表理解:

from numba.typed import List
numba_list = List()
py_list = ["a", "b", "c"]
[numba_list.append(e) for e in py_list]
print(numba_list)

输出:

ListType[unicode_type]([a, b, c])

Numba Deprecation Notice 的例子中得出这个结论。

我正在开发 numba 0.49.1,您可以在其中通过构造传递列表。

py_list = [2,3,5]    
number_list = numba.typed.List(py_list)