-1

I replaced a Python list with a Numba int32 list and its concatenation behaviour wasn't as expected.

For example in Python: if a = [1,2,3] and b = [4,5,6] then a += b gives [1, 2, 3, 4, 5, 6].

In Numba, on the other hand: if a = numba.int32([1,2,3]) and b = numba.int32([4,5,6]) then a += b gives array([5, 7, 9], dtype=int32).

Is there a way I can easily concatenate Numba lists as we do in Python? or is the only solution to write a function that loops over the two arrays and creates another one?

Thanks.

user3353185
  • 127
  • 1
  • 12
  • Does this answer your question? [Concatenating two one-dimensional NumPy arrays](https://stackoverflow.com/questions/9236926/concatenating-two-one-dimensional-numpy-arrays) – Woodford Aug 04 '21 at 20:37
  • I just figured out that if I used List from numba.typed, it behaves as you would expect with the increment operator. Funnily you can't add two numba.typed lists though. Thanks for the responses. – user3353185 Aug 04 '21 at 20:42
  • In reply to Woodford's question, thanks for the suggestion, but it wasn't immediately obvious to me that Numba and Numpy arrays were compatible. I'm keeping my question since it might help others to whom might not be aware of the compatibility. – user3353185 Aug 04 '21 at 20:52

2 Answers2

1

use the concatenate function. https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html

import numpy as np
a = numba.int32([1,2,3])
b = numba.int32([4,5,6])
c = np.concatenate((a,b))
Ricardo
  • 350
  • 1
  • 10
  • Accepted this over Sup's correct answer since the method works on numba.typed List too. – user3353185 Aug 04 '21 at 20:44
  • @user3353185 It did not work for numba.typed.List. At least with numba==0.56.0, I am receiving the following... TypeError: np.concatenate(): expecting a non-empty tuple of arrays – Larry Panozzo Oct 14 '22 at 21:30
1

Use list()

a = numba.int32(list(a)+list(b))
Sup
  • 191
  • 5