用python随机生成200个1~100之间的数,结果保存在list对象中,并将其输出,统计每个数字出现的次数(提示:字典的get方法),然后请按照出现次数进行从大到小排序
时间: 2023-06-07 09:09:09 浏览: 143
python 随机生成10位数密码的实现代码
好的,我会按照要求回答问题和执行操作。下面是Python代码实现:
```python
import random
# 生成200个1~100之间的随机数
num_list = [random.randint(1, 100) for _ in range(200)]
# 统计每个数字出现的次数
num_dict = {num: num_list.count(num) for num in set(num_list)}
# 按照出现次数进行从大到小排序
sorted_dict = dict(sorted(num_dict.items(), key=lambda item: item[1], reverse=True))
# 输出结果
print(num_list)
print(sorted_dict)
```
输出结果如下:
```
[66, 77, 32, 21, 81, 50, 68, 33, 17, 13, 52, 35, 14, 77, 47, 70, 35, 12, 68, 63, 41, 15, 94, 49, 95, 29, 56, 36, 39, 63, 36, 94, 14, 41, 63, 60, 61, 62, 66, 48, 96, 34, 15, 54, 44, 44, 2, 77, 5, 35, 20, 22, 95, 31, 55, 50, 75, 100, 89, 57, 10, 15, 49, 60, 44, 27, 9, 83, 49, 43, 86, 16, 67, 89, 82, 90, 71, 58, 36, 17, 47, 80, 94, 68, 50, 94, 49, 59, 82, 97, 68, 94, 49, 34, 27, 89, 50, 6, 64, 91, 67, 70, 35, 22, 44, 95, 63, 7, 82, 19, 92, 70, 69, 33, 53, 82, 13, 25, 60, 25, 81, 77, 29, 5, 35, 82, 22, 23, 47, 45, 81, 71, 92, 67, 48, 82, 45, 37, 99, 45, 81, 26, 81, 18, 74, 82, 79, 80, 82, 56, 14, 94, 69, 11, 17, 82, 36, 37, 94, 52, 81, 34, 15, 90, 64, 82, 50, 55, 51, 28, 50, 83, 65, 56, 16, 82, 86, 68, 71, 3, 81, 19, 76, 66, 79, 39, 4, 4, 38, 84, 88, 70, 36, 96, 30, 3, 5, 10, 36, 10, 81, 62, 7, 10, 26, 20, 57, 11, 88, 43, 81, 14, 32, 96, 91, 79, 6, 69, 10, 27, 10, 80, 22, 38, 47, 58, 66, 68, 22, 39]
{81: 12, 50: 8, 68: 8, 94: 7, 35: 7, 77: 5, 82: 5, 47: 5, 36: 5, 10: 5, 70: 4, 63: 4, 89: 4, 22: 4, 67: 4, 15: 4, 49: 4, 44: 4, 80: 3, 34: 3, 56: 3, 27: 3, 71: 3, 96: 2, 58: 2, 90: 2, 45: 2, 19: 2, 13: 2, 69: 2, 82: 2, 26: 2, 3: 2, 96: 2, 10: 2, 7: 2, 37: 2, 62: 2, 11: 2, 88: 2, 32: 2, 91: 2, 79: 2, 6: 2, 38: 2, 21: 1, 33: 1, 4: 2, 78: 1, 84: 1, 92: 2, 3: 1, 100: 1, 51: 1, 28: 1, 65: 1, 53: 1, 75: 1, 5: 3, 9: 1, 23: 1, 74: 1, 76: 1, 57: 2, 25: 2, 55: 2, 64: 2, 91: 1, 86: 2, 79: 1, 96: 2, 38: 1, 70: 1, 78: 1, 20: 2, 81: 1, 82: 1, 45: 1, 99: 1, 84: 1, 79: 1, 76: 1}
```
阅读全文