Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a numba typed List without looping over a python list

Tags:

python

numba

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'

like image 236
RK1 Avatar asked Feb 12 '20 10:02

RK1


People also ask

Does Numba work with list?

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 ).

Is Numba faster than Cython?

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.

What is a reflected list Python?

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.

What is NJIT Numba?

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.


1 Answers

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)
like image 141
Tobias Senst Avatar answered Nov 15 '22 00:11

Tobias Senst