0

i have an array [[[1],[2]]] and i want to add [3] to get [[[1],[2],[3]]]

what i've tried so far

my_array = numpy.array([[[1],[2]]])
print(my_array.shape, my_array)

to_append = numpy.array([3])
print(to_append.shape, to_append)

# i want [[[1],[2],[3]]]
my_array[0] = numpy.append(my_array[0], to_append)

but i get

ValueError: could not broadcast input array from shape (3) into shape (2,1)

3 Answers 3

3

you can use numpy.concatenate:

>>> np.concatenate([my_array, [[[3]]]], 1)
array([[[1],
        [2],
        [3]]])
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe numpy.concatenate could work here but you have to make sure the arrays are the same in one dimension

Comments

1
numpy.append(my_array, to_append[None,:,None], 1)

Result

array([[[1],
        [2],
        [3]]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.