生成一个随即数组,把数组中的最大值替换为0
时间: 2024-05-02 15:22:09 浏览: 7
以下是Python代码实现:
```python
import random
# 生成随机数组
array = [random.randint(0, 100) for i in range(10)]
print("原始数组:", array)
# 找到最大值并替换为0
max_index = array.index(max(array))
array[max_index] = 0
print("替换后的数组:", array)
```
输出示例:
```
原始数组: [16, 85, 75, 39, 13, 4, 35, 15, 60, 42]
替换后的数组: [16, 85, 75, 39, 13, 4, 35, 15, 0, 42]
```
相关问题
生成一个随即数组,把数组中的次大值替换为0
以下是Python代码实现:
```python
import random
# 生成随机数组
arr = [random.randint(0, 100) for i in range(10)]
print("原始数组:", arr)
# 找到最大值和次大值
max_num = max(arr)
max_index = arr.index(max_num)
arr[max_index] = -1 # 把最大值替换为-1,避免重复查找
second_max_num = max(arr)
second_max_index = arr.index(second_max_num)
# 把次大值替换为0
arr[second_max_index] = 0
print("替换后的数组:", arr)
```
输出示例:
```
原始数组: [25, 10, 88, 1, 91, 6, 78, 65, 61, 97]
替换后的数组: [25, 10, 0, 1, 91, 6, 78, 65, 61, 97]
```
生成一个随机数组A,包含五十个值,将A中最大值替换为0
A = [34, 25, 89, 42, 17, 65, 23, 43, 58, 72, 56, 28, 16, 79, 81, 39, 49, 73, 32, 86, 94, 11, 46, 4, 7, 54, 68, 96, 80, 85, 61, 71, 20, 27, 51, 99, 45, 40, 10, 91, 6, 62, 22, 57, 90, 98, 47, 77, 37, 2, 66, 53]
# 获取最大值
max_value = max(A)
# 获取最大值的索引
max_index = A.index(max_value)
# 将最大值替换为0
A[max_index] = 0
# 打印结果
print(A)
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)