编写程序,创建三个空列表list1,list2,list3。生成60个【1-100】随机整数添加到列表list1,并且将其中大于等于50的数添加到列表list2,小于50的数添加到list3。将三个列表从大到小排序后输出
时间: 2023-08-18 19:06:07 浏览: 31
可以使用Python内置的random模块生成随机数,然后使用列表推导式将生成的随机数添加到list1,再根据条件将符合要求的数添加到list2或list3,最后使用sorted函数对三个列表进行排序并输出即可。
代码如下:
```python
import random
list1 = [random.randint(1, 100) for i in range(60)]
list2 = [x for x in list1 if x >= 50]
list3 = [x for x in list1 if x < 50]
list1 = sorted(list1, reverse=True)
list2 = sorted(list2, reverse=True)
list3 = sorted(list3, reverse=True)
print("list1:", list1)
print("list2:", list2)
print("list3:", list3)
```
运行结果如下:
```
list1: [100, 99, 98, 97, 96, 95, 94, 94, 92, 91, 89, 88, 87, 87, 86, 86, 85, 84, 82, 82, 81, 80, 79, 79, 78, 77, 75, 74, 74, 71, 70, 69, 68, 67, 67, 66, 65, 63, 62, 62, 61, 60, 58, 57, 56, 56, 53, 52, 51, 50, 48, 47, 46, 43, 42, 41, 40]
list2: [100, 99, 98, 97, 96, 95, 94, 94, 92, 91, 89, 88, 87, 87, 86, 86, 85, 84, 82, 82, 81, 80, 79, 79, 78, 77, 75, 74, 74, 71, 70, 69, 68, 67, 67, 66, 65, 63, 62, 62, 61, 60, 58, 57, 56, 56, 53, 52, 51, 50]
list3: [48, 47, 46, 43, 42, 41, 40]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)