How to operate on list generator using numba

女生的网名这么多〃 提交于 2019-12-25 02:23:17

问题


I'm trying to use a simple code that tries to use numba and list generator and I get an error executing the following code.

@numba.jit(nopython=True, nogil=True)
def perform_search(simple_list, number):
    gen = (ind for ind in xrange(len(simple_list)) if simple_list[ind] != number)
    return next(gen)

x = [1,1,1,2,1,3]
perform_search(x, 1)

When I execute the above code I get a ValueError, however, when just use the decorator @numba.jit, I get a a LoweringError.

Please help me to perform this simple search using generator (or otherwise). Thanks in advance


回答1:


What you have

gen = (ind for ind in xrange(len(simple_list)) if simple_list[ind] != number)

is a generator expression, which is not supported by numba at the moment.

If you use square brackets instead, like:

gen = [ind for ind in xrange(len(simple_list)) if simple_list[ind] != number]

then it is a list-comprehension and numba can support it. With that change, gen is a list and you can index it (i.e. gen[0]).

EDITED:

The following code is a suggestion from the user sklam in gitter, that I'm updating here.

@numba.jit(nopython=True)
def the_gen(simple_list, number):
    for ind in range(len(simple_list)):
        if simple_list[ind] != number:
            yield ind


@numba.jit(nopython=True, nogil=True)
def perform_search(simple_list, number):
    for i in the_gen(simple_list, number):
        print(i)

If you do the above way, you will be able to do with a generator (so gains in memory and time) since generator-expression is currently not supported by numba.



来源:https://stackoverflow.com/questions/48118734/how-to-operate-on-list-generator-using-numba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!