튜플 목록(즉, 튜플 목록)에서 최대 요소를 찾아야 하는 경우 'max' 메서드와 'operator.itemgetter' 메서드를 사용할 수 있습니다.
itemgetter는 피연산자에서 특정 항목을 가져옵니다.
'max' 메소드는 iterable에 인수로 전달되는 iterable에 존재하는 최대값을 제공합니다.
아래는 동일한 데모입니다 -
예시
from operator import itemgetter
my_list = [('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)]
print ("The list is : ")
print(my_list)
my_result = max(my_list, key = itemgetter(1))[0]
print ("The name that has the maximum value is : ")
print(my_result) 출력
The list is :
[('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)]
The name that has the maximum value is :
Min 설명
- 필수 라이브러리를 가져옵니다.
- 튜플 목록이 정의되어 콘솔에 표시됩니다.
- 'max' 방법은 목록을 살펴보고 목록에 있는 모든 튜플의 첫 번째 요소로 키를 지정하는 데 사용됩니다.
- 이 결과는 값에 할당됩니다.
- 콘솔에 출력으로 표시됩니다.