티스토리 뷰
myFunc은 온전한 값을 리턴하는 함수라고 가정한다.
input_list의 결과 값 중 가장 작은 myFunc을 리턴하는 element는 무엇일까?
내 코드
input_list = [5, 10, 15, 20, 25]
myFunc_result = []
for input in input_list:
myFunc_result.append(myFunc(input))
smallest = 0
for index in range(len(myFunc_result)):
if myFunc_result[smallest]>myFunc_result[index]:
smallest = index
smallest_element = input_list[smallest]
더 효율적인 코드
myFunc_result = {result: myFunc(input) for input in input_list}
smallest_element = min(myFunc_result, key=myFunc_result.get)
'언어 > Python' 카테고리의 다른 글
list를 torch로 변환하기 (0) | 2022.06.06 |
---|---|
List Comprehension Practice (수정 보충 필요) (0) | 2021.01.07 |
쓰임새를 모르는 Python object 용도 찾기 (0) | 2021.01.01 |
exercise-strings-and-dictionaries (0) | 2021.01.01 |
List Comprehensions in Python (0) | 2021.01.01 |