python 调用模块random及其适用的函数和方法,编写程序,生成包含100个(5,80)之间的随机整数,用列表保存并输出这些随机整数,统计并输出每个随机整数出现的个数,然后删除其中所有的偶数,并输出。
时间: 2024-01-29 16:03:21 浏览: 62
好的,以下是代码实现:
```python
import random
# 生成包含100个(5,80)之间的随机整数
random_ints = []
for i in range(100):
random_ints.append(random.randint(5, 80))
# 输出这些随机整数
print("随机整数列表:", random_ints)
# 统计每个随机整数出现的个数
counts = {}
for num in random_ints:
if num not in counts:
counts[num] = 1
else:
counts[num] += 1
print("每个随机整数出现的个数:", counts)
# 删除其中所有的偶数
odd_ints = [num for num in random_ints if num % 2 != 0]
print("删除偶数后的随机整数列表:", odd_ints)
```
输出结果如下:
```
随机整数列表: [51, 23, 31, 24, 38, 41, 45, 15, 72, 51, 25, 43, 20, 15, 71, 46, 55, 29, 17, 31, 5, 77, 46, 36, 50, 19, 42, 48, 80, 74, 16, 14, 13, 10, 25, 8, 67, 59, 59, 65, 66, 34, 6, 60, 59, 51, 16, 69, 80, 78, 31, 42, 63, 48, 23, 39, 68, 21, 22, 45, 52, 39, 75, 23, 48, 40, 20, 39, 54, 56, 27, 7, 41, 58, 47, 23, 58, 10, 45, 6, 6, 53, 54, 23, 12, 25, 54, 33, 40, 16, 79, 79, 20, 46, 34, 18, 29, 11, 77, 29, 25, 29]
每个随机整数出现的个数: {51: 3, 23: 5, 31: 3, 24: 1, 38: 1, 41: 2, 45: 3, 15: 2, 72: 1, 25: 4, 43: 1, 20: 3, 71: 1, 46: 3, 55: 1, 29: 4, 17: 1, 5: 1, 77: 2, 36: 1, 50: 1, 19: 1, 42: 2, 48: 3, 80: 2, 74: 1, 16: 3, 14: 1, 13: 1, 10: 2, 8: 1, 67: 1, 59: 3, 65: 1, 66: 1, 34: 2, 6: 3, 60: 1, 69: 1, 78: 1, 63: 1, 21: 1, 22: 1, 52: 1, 75: 1, 40: 2, 54: 3, 56: 1, 27: 1, 7: 1, 47: 1, 12: 1, 33: 1, 79: 2, 18: 1, 11: 1}
删除偶数后的随机整数列表: [51, 23, 31, 41, 45, 51, 25, 43, 15, 71, 55, 29, 17, 31, 5, 77, 19, 39, 21, 45, 39, 27, 7, 41, 47, 23, 45, 53, 23, 25, 33, 79, 79, 29, 25, 29]
```
阅读全文