假设你有一个包含各个类别总数的字典,可以使用以下代码来计算各类别的百分比:
# 假设你有一个包含各个类别总数的字典,例如: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}%')