创建一个10× 10的随机数组,并显示输出其最大值和最小值。
时间: 2024-05-06 15:21:26 浏览: 21
好的,这个问题属于技术领域。下面是生成随机数组并输出最大值和最小值的代码,供您参考:
```python
import random
# 生成10*10的随机数组
arr = [[random.randint(0, 100) for j in range(10)] for i in range(10)]
print(arr)
# 输出最大值和最小值
max_val = max(map(max, arr))
min_val = min(map(min, arr))
print('最大值: ', max_val)
print('最小值: ', min_val)
```
希望能对您有所帮助。如果还有其他问题,欢迎继续提问。
相关问题
创建一个10x10的随机数组并找到它的最大值和最小值。
以下是Python代码实现:
```python
import random
# 创建10x10的随机数组
arr = [[random.randint(1, 100) for j in range(10)] for i in range(10)]
# 输出数组
print("随机数组:")
for i in range(10):
for j in range(10):
print(arr[i][j], end=" ")
print()
# 找到最大值和最小值
max_val = arr[0][0]
min_val = arr[0][0]
for i in range(10):
for j in range(10):
if arr[i][j] > max_val:
max_val = arr[i][j]
if arr[i][j] < min_val:
min_val = arr[i][j]
# 输出结果
print("最大值:", max_val)
print("最小值:", min_val)
```
输出结果类似于:
```
随机数组:
56 11 78 98 39 68 22 61 44 94
9 79 49 76 52 19 94 74 27 23
58 23 27 4 79 83 60 16 62 5
7 5 37 25 22 45 51 39 25 89
17 31 79 95 72 2 3 41 31 100
23 27 23 90 1 27 73 37 56 9
5 80 14 7 26 89 46 36 41 94
65 64 56 17 70 9 77 54 86 66
55 35 89 26 62 19 3 37 14 70
40 22 94 41 34 88 15 35 85 95
最大值: 100
最小值: 1
```
创建一个5×5的数组,并找出最大值与最小值
好的,我可以帮你完成这个任务。以下是Python代码实现:
```python
import numpy as np
# 创建5x5的随机数组
arr = np.random.randint(1, 101, size=(5, 5))
print("数组:\n", arr)
# 找到最大值和最小值
max_val = np.max(arr)
min_val = np.min(arr)
# 输出结果
print("最大值:", max_val)
print("最小值:", min_val)
```
这段代码使用NumPy模块创建了一个5x5的随机数组,并找到了数组中的最大值和最小值。你可以在代码中修改数组的大小或范围来满足你的需求。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)