Array
You can add an array to a python array using the `.append`

method.

```
>>> arr = []
>>> arr.append([1, 2, 3])
>>> arr.append([4, 5])
>>> arr
[[1, 2, 3], [4, 5]]
```

It is a two-dimensional array with free row lengths.

numpy.ndarray If you try to add it in the same way with numpy, it will not be a 2D array.

```
>>> arr = np.array([])
>>> arr = np.append(arr, np.array([1, 2, 3]))
>>> arr = np.append(arr, np.array([4, 5]))
>>> arr
array([ 1., 2., 3., 4., 5.])
```

To add a row to the end of numpy as a two-dimensional array, you need to write:

```
>>> arr = np.empty((0,3), int)
>>> arr = np.append(arr, np.array([[1, 2, 3]]), axis=0)
>>> arr = np.append(arr, np.array([[4, 5, 0]]), axis=0)
>>> arr
array([[1, 2, 3],
[4, 5, 0]])
```

point

--Initialize with `np.empty`

--The number of elements in the array to be added matches the length of the initialized row (3 in the above example).
--`ndarray`

, which is the second argument of` np.append`

, is a multiple array.
--Do not forget ʻaxis = 0`

```
>>> arr
array([[1, 2, 3],
[4, 5, 0]])
>>> arr[0,1]
2
>>> arr[1,:2]
array([4, 5])
>>> arr[:, 1]
array([2, 5])
>>>
```

Slicing is easy and convenient.

Recommended Posts