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 3 years have passed since last update.

【python】2番めに小さな値を求める。

Posted at

#【python】2番めに小さな値を求める。
個人メモです。

・与えられた数値と名前を入れ子の配列化(nested list)
・2番目に小さい数値を持つ人の名前をアルファベット順で抜き出す

▼sample input

5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39

▼my answer

if __name__ == '__main__':

    #値を格納する配列を準備
    nameScores=[]
    scores=[]

    for _ in range(int(input())):
        name = input()
        score = float(input())
    
        scores.append(score)
        nameScore=[name, score]
        nameScores.append(nameScore)
    
  #名前のアルファベット順にソート
    nameScores = sorted(nameScores) 
        
    #2番めに小さい値求める
    secondlow = sorted(set(scores))[1]
    
    #最小値を持つ名前を抜き出す
    names=[]
    for nameScore in nameScores:
        if nameScore[1]==secondlow:
            print(nameScore[0])

配列.append(値)
配列に値を追加

sorted(配列)
配列を昇順ソート(ネストの場合は0番目の要素)
アルファベット=a~
数値=0~

オプションで降順、キーとなる要素を指定可能。
・降順: reverse=True
・キー指定:ラムダ式で指定。
key=lambda val:val[インデックス番号]
key=lambda x:x[キー名]

set()
重複する値を削除
set型

0
0
1

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?