output = {
"都市": 0,
"娯楽": 0,
"活発": 0,
"食事": 10,
"お金": 9
}
change = {
"都市":"自然",
"娯楽":"学び",
"活発":"のんびり"
}
# 各カテゴリに特定の条件に基づいて偏差を計算する関数
def calculate_custom_deviation(scores):
center = 5
deviations = {}
for category, score in scores.items():
if category in ['食事', 'お金']:
# '食事' と 'お金' の場合、5からの正の偏差を計算する
deviations[category] = max(0, score - center)
elif category == "都市":
# 他のカテゴリーの場合、5からの絶対偏差を計算する
if score - center > 0:
deviations[category] = abs(score - center)
else:
deviations[change[category]] = abs(score - center)
elif category == "娯楽":
if score - center > 0:
deviations[category] = abs(score - center)
else:
deviations[change[category]] = abs(score - center)
elif category == "活発":
if score - center > 0:
deviations[category] = abs(score - center)
else:
deviations[change[category]] = abs(score - center)
else:
# 他のカテゴリーの場合、5からの絶対偏差を計算する
deviations[category] = abs(score - center)
# 値の大きい上位2つを抽出
top2_deviations = dict(sorted(deviations.items(), key=lambda item: item[1], reverse=True)[:2])
return top2_deviations
# すべてのカテゴリーに対するカスタム偏差を計算する
custom_deviations = calculate_custom_deviation(output)
print(custom_deviations)
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme