使用np.random.randint创建一个10*10的ndarray对象,并分别打印出最大和最小元素的坐标
时间: 2024-02-23 10:01:54 浏览: 29
好的,以下是代码实现:
```python
import numpy as np
# 创建10*10的随机整数数组
arr = np.random.randint(0, 100, size=(10, 10))
# 打印数组
print(arr)
# 获取最大值和最小值的坐标
max_index = np.unravel_index(arr.argmax(), arr.shape)
min_index = np.unravel_index(arr.argmin(), arr.shape)
# 打印最大值和最小值的坐标
print("最大值的坐标为:", max_index)
print("最小值的坐标为:", min_index)
```
输出结果类似于:
```
[[24 47 9 22 75 1 30 8 58 36]
[33 7 1 78 4 5 2 9 6 86]
[35 2 5 1 6 67 6 9 71 2]
[ 7 67 3 77 83 5 3 4 6 19]
[ 9 55 6 23 9 25 76 6 73 9]
[39 20 91 2 61 92 2 47 34 87]
[ 7 60 8 6 48 6 31 6 39 68]
[ 1 72 9 6 16 27 4 91 58 62]
[ 9 33 77 37 37 5 24 48 19 89]
[58 7 29 9 35 31 26 34 71 20]]
最大值的坐标为: (5, 5)
最小值的坐标为: (0, 5)
```
其中,`np.unravel_index` 函数可以将一维的数组下标转换为多维数组下标,这样就可以方便地得到在多维数组中的坐标位置了。