Eric Chen - 5 months ago 17

Python Question

I have an array A:

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

[1, 1, 1],

[2, 2, 2]])

and an array B:

`array([[1, 0],`

[1, 0],

[0, 1]])

I want to make array B as the last column of array A, so I want the result array (let's call it C) to look like this:

`array([[1, 2, 3, [1, 0]],`

[1, 1, 1, [1, 0]],

[2, 2, 2, [0, 1]]])

I tried:

`np.insert(a,-1,b,axis=1)`

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

Answer

Maybe that's what you're looking for:

```
import numpy as np
a = np.array([[1, 2, 3],
[1, 1, 1],
[2, 2, 2]])
b = np.array([[1, 0],
[1, 0],
[0, 1]])
np.hstack([a,b])
```

Which results in:

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