@beginner_programan

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

python 最大値を表示させるにはどうすればいいでしょうか?

解決したいこと

a=1,b=5,c=4,d=2,e=9(それぞれの数字は変わる)があり、abcdeで数値が最大であるアルファベットを表示させたいです。この場合ではe=9が最大なのでeと表示させたいです。どのようなプログラムを書けば良いかわかりません。どのようなプログラムを書けば良いでしょうか?pythonを使ってます。

0 likes

3Answer

a,b,c,d,eが変数でなくてもいいなら、シンプルにこんな感じでしょうか。

d = {'a': 1, 'b': 5, 'c': 4, 'd': 2, 'e': 9}

alphabet = max(d, key=d.get)

print(alphabet)  # -> e
2Like

Comments

  1. シンプルで良かったです。ありがとうございます。

このような感じでどうでしょうか

import random
list = [(chr(ord('a')+i), random.randint(0, 100)) for i in range(5)]
for x in list: print(f"{x[0]}={x[1]}")
print("max : " + max(list, key=(lambda x: x[1]))[0])
1Like

あ、元の仕様がa-eの変数に入れないといけないのでこんな感じの方がいいですかね
index取得すればわざわざタプルで文字入れる必要もなかったですね…

from random import randint
a = randint(0, 100)
b = randint(0, 100)
c = randint(0, 100)
d = randint(0, 100)
e = randint(0, 100)
print(f"a={a},b={b},c={c},d={d},e={e}")
list = [a, b, c, d, e]
print(chr(97+list.index(max(list))))
1Like

Comments

  1. このプログラムも使えそうなのでありがとうございます。

Your answer might help someone💌