GlaceCelery
GlaceCelery

Reputation: 1043

How to create a typed dict in Numba where (key, value) = (str, list)

I am trying to create a dictionary in nopython mode. This is what I have:

import numba

@numba.njit()
def func():

    d = numba.typed.Dict.empty(
        key_type=numba.types.unicode_type,
        value_type=numba.ListType(np.float64)
    )

    d["a"] = [1, 1]

    return d

print(func())

Error:

Invalid use of Function(<class 'numba.types.containers.ListType'>) with argument(s) of type(s): (Function(<class 'float'>))
 * parameterized
In definition 0:
    TypeError: typer() takes 0 positional arguments but 1 was given

Upvotes: 2

Views: 1706

Answers (1)

Zuku
Zuku

Reputation: 1210

Seems that there's need to declare ListType outside of njit block (at least I'm not able to do that differently). Also you have to append elements to list one by one. Try this code:

import numba

list_type = numba.types.ListType(numba.types.float64)

@numba.njit()
def func():
    d = numba.typed.Dict.empty(
        key_type=numba.types.unicode_type,
        value_type=list_type
    )

    d["a"] = numba.typed.List.empty_list(numba.types.float64)
    d["a"].append(1)
    d["a"].append(1)

    return d

print(func())

Output:

{a: [1.0, 1.0]}

Upvotes: 5

Related Questions