NumPy一维数组索引: 一维数组切片: 二维数组索引: 二维数组切片(取第 2 列):二维数组切片(取第 2,3 行):
时间: 2023-12-18 15:04:15 浏览: 124
python numpy数组的索引和切片的操作方法
5星 · 资源好评率100%
一维数组索引:
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[0]) # 输出:1
print(arr[2]) # 输出:3
```
一维数组切片:
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:4]) # 输出:[2 3 4]
```
二维数组索引:
```python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[0, 0]) # 输出:1
print(arr[1, 2]) # 输出:6
```
二维数组切片(取第 2 列):
```python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[:, 1]) # 输出:[2 5 8]
```
二维数组切片(取第 2,3 行):
```python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[1:3, :]) # 输出:[[4 5 6] [7 8 9]]
```
阅读全文