Python nummpy 创建包含随机数值的10×10数组,找最大值和最小值
时间: 2023-07-15 07:15:37 浏览: 245
numpy找出array中的最大值,最小值实例
可以使用NumPy中的random模块创建一个10x10的随机数组,然后使用np.max和np.min函数分别找到最大值和最小值。
```python
import numpy as np
# 创建一个10x10的随机数组
arr = np.random.rand(10, 10)
# 找到最大值和最小值
max_val = np.max(arr)
min_val = np.min(arr)
print("随机数组:\n", arr)
print("最大值:", max_val)
print("最小值:", min_val)
```
输出结果类似于:
```
随机数组:
[[0.21011535 0.4702648 0.72564963 0.37763969 0.38455544 0.85866038
0.62814876 0.98692907 0.41816225 0.70710039]
[0.10510936 0.47219806 0.40159631 0.53917169 0.50804212 0.86535543
0.10627816 0.5639655 0.49004089 0.87117202]
[0.30475362 0.96682548 0.66152096 0.30413553 0.33206844 0.55799124
0.75757382 0.88338774 0.58620637 0.92919612]
[0.41356781 0.82121169 0.4849927 0.71054533 0.88811278 0.76690468
0.38608419 0.7614684 0.20093452 0.26976301]
[0.94201268 0.27928378 0.22382573 0.51067911 0.10613765 0.96295912
0.51452166 0.29836526 0.23532166 0.16783197]
[0.36453931 0.91922849 0.67029496 0.24665827 0.40293368 0.46444714
0.65020232 0.12714308 0.03947211 0.86185306]
[0.03945995 0.54358977 0.32877955 0.75670827 0.5230202 0.83888141
0.2846411 0.58515558 0.93874299 0.23683545]
[0.76746706 0.4882545 0.35071445 0.63463764 0.75476835 0.34858056
0.57091225 0.31822671 0.20110202 0.14223255]
[0.82193521 0.76693428 0.01590176 0.20590822 0.93737132 0.58293747
0.01303016 0.64906419 0.95737756 0.96452697]
[0.38946557 0.73078983 0.7316306 0.11818059 0.05972853 0.95799856
0.78997581 0.10459538 0.33909714 0.06840513]]
最大值: 0.9869290715670428
最小值: 0.013030155184813098
```
阅读全文