假设你有一个包含各个类别总数的字典,可以使用以下代码来计算各类别的百分比:
# 假设你有一个包含各个类别总数的字典,例如:category_counts = {'类别1': 50, '类别2': 30, '类别3': 20}
category_counts = {'类别1': 50, '类别2': 30, '类别3': 20}
# 计算总数
total_count = sum(category_counts.values())
# 计算各类别的百分比
category_percentages = {category: (count / total_count) * 100 for category, count in category_counts.items()}
# 打印各类别的百分比
for category, percentage in category_percentages.items():
print(f'{category}: {percentage:.2f}%')
文章介绍了如何使用Python计算给定字典中各类别(如类别1,类别2,类别3)的百分比,通过总数量求得每个类别的占比并打印结果。
265

被折叠的 条评论
为什么被折叠?



