I want to use a numba.typed.List
(going to call it List
) to pass into my function which is wrapped in njit
. However this List
should be created from an existing python list.
When I look at the documentation it seems the way you create a List
is to initialize it and then append elements to it. However this requires you to loop over an already existing list in python which seems inefficient for large lists.
For example:
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 ?
I am using numba.__version__
= '0.47.0'
Lists must be strictly homogeneous: Numba will reject any list containing objects of different types, even if the types are compatible (for example, [1, 2.5] is rejected as it contains a int and a float ).
Following benchmark result shows Cython and Numba library can significantly speed up Python code. Computation time for Python and Cython increase much faster compared to Numba.
Deprecation of reflection for List and Set types. Reflection (reflection) is the jargon used in Numba to describe the process of ensuring that changes made by compiled code to arguments that are mutable Python container data types are visible in the Python interpreter when the compiled function returns.
Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops. The most common way to use Numba is through its collection of decorators that can be applied to your functions to instruct Numba to compile them.
I am working on numba 0.49.1 where you can pass the list by the construction.
py_list = [2,3,5]
number_list = numba.typed.List(py_list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With