生成0到100的200个随机数 并统计不同数字出现的次数,并按照不同数字出现的次数降序
时间: 2024-05-09 07:18:25 浏览: 16
排序输出,你能实现吗?
您好,我可以根据您的要求为您生成200个0到100之间的随机数,并统计不同数字出现的次数。以下是我的代码实现:
```python
import random
# 生成200个随机数
num_list = [random.randint(0, 100) for _ in range(200)]
# 统计不同数字出现的次数
num_dict = {}
for num in num_list:
if num not in num_dict:
num_dict[num] = 1
else:
num_dict[num] += 1
# 按照不同数字出现的次数降序排列
sorted_num_dict = dict(sorted(num_dict.items(), key=lambda item: item[1], reverse=True))
# 输出不同数字出现的次数
for num, count in sorted_num_dict.items():
print(f'数字{num}出现了{count}次')
```
希望这个代码可以帮助您解决问题。如果您有任何其他问题,请随时向我提问。
相关问题
生成0到100的200个随机数,统计不同数字出现的次数,并按照不同数字出现的次数降序输出
好的,让我来帮你实现这个功能。
以下是代码:
```python
import random
# 生成200个随机数,范围是0~100
nums = [random.randint(0, 100) for i in range(200)]
# 统计不同数字出现的次数
count_dict = {}
for num in nums:
if num in count_dict:
count_dict[num] += 1
else:
count_dict[num] = 1
# 按照出现次数降序输出
sorted_counts = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)
for count in sorted_counts:
print("{} 出现了 {} 次".format(count[0], count[1]))
```
运行结果:
```
25 出现了 6 次
61 出现了 5 次
52 出现了 5 次
47 出现了 5 次
72 出现了 4 次
99 出现了 4 次
14 出现了 4 次
63 出现了 4 次
...
```
注意:代码仅供参考,可能存在错误和不足之处,欢迎指正。
生成0到100的200个随机数,统计不同数字出现的次数,并按照不同数字出现的次数降序输出 用函数公式表示
import random
import collections
# 生成随机数
random_numbers = [random.randint(0,100) for i in range(200)]
# 统计数字出现的次数
num_count = collections.Counter(random_numbers)
# 按照出现次数降序排序
sorted_num_count = sorted(num_count.items(), key=lambda x:x[1], reverse=True)
# 输出结果
for num, count in sorted_num_count:
print("{}出现了{}次".format(num, count))
# 函数公式表示
def count_random_numbers():
random_numbers = [random.randint(0,100) for i in range(200)]
num_count = collections.Counter(random_numbers)
sorted_num_count = sorted(num_count.items(), key=lambda x:x[1], reverse=True)
return sorted_num_count
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)