0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

自分用YKKK

Posted at

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)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?