python-3.xnumpynumbanumba-pro

pass list of list into numba function in no python mode, if element in list_of_list[0] doe not work


See the following minimum code,

import numba
list_of_list = [[1, 2], [34, 100]]
@numba.njit()
def test(list_of_list):
    if 1 in list_of_list[0]:
        return 'haha'

test(list_of_list)

This won't work and it seems that list_of_list[0] is no longer behaves like a list during compile. However, the following code works:

list_of_list = [[1, 2], [34, 100]][0] # this is a list NOW!
@numba.njit()
def test(list_of_list):
    if 1 in list_of_list:
        return 'haha'

test(list_of_list)

This time, what I pass into is actually list, NOT list of list. Then it works. It seems for i in list works in numba, not for i in list_of_list.

In my use case, passing list of list or array like 2d data into numba function is common. Sometimes I only need one element in the list, which is dynamically determined in the program.

In order to make it work, I actually worked out a solution: making list_of_list flattened into a long list, then use linear index to extract one element in original list_of_list.

I am asking here, is there other alternative solutions?


Solution

  • The in method works on sets. Returning a string can also cause some problems.

    Working example

    import numba as nb
    import numpy as np
    
    array_2D = np.array([[1, 2], [34, 100]])
    
    @nb.njit()
    def test(array_2D):
        if 1 in set(array_2D[0]):
            #Strings also causes sometimes problems
            #return 'haha'
            return(1)
        else:
            return(-1)