2
4

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でn番目に大きい値を取り出す方法

Last updated at Posted at 2020-01-27

競技プログラミングとかで割と使うので備忘録程度に。
2パターンあります

1.重複を含む

Input

numbers = [9,2,5,4,7]
numbers.sort()
print(numbers)
print(numbers[-2])

Output

[2,4,5,7,9]
7

2.重複を含まない

Input

numbers = [8, 8, 1, 4, 3]
a = sorted(set(numbers))
print(a)
print(a[-2])

Output

[1 ,3 ,4 ,8]
4
2
4
2

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?