-
[Python] 내장 함수 : mapToday's/DevelopStory 2021. 6. 15. 01:19
입력 형태 : map(함수, 자료형)
반복되는 자료형의 각 요소를 함수가 수행한 결과로 묶어서 돌려주는 파이썬 내장 함수 중 하나이다.
예시 :
# two_times.py
def two_times(numberList):
result = [ ]
for number in numberList:
result.append(number * 2)
return result
result = two_times([ 1, 2, 3, 4 ])
print(result)two_times 함수는 리스트 요소를 입력받아 각 요소에 2를 곱한 결괏값을 돌려준다.
실행 결과 : [ 2, 4, 6, 8 ]위 예시를 map 함수의 경우로 나타내면 다음과 같다.
def two_times( x ):
return x*2
list(map(two_times, [ 1, 2, 3, 4 ]))
[ 2, 4, 6, 8 ]리스트의 각 요소들이 함수의 입력값으로 들어가고
x*2의 과정이 각 요소들을 거치게 된다.'Today's > DevelopStory' 카테고리의 다른 글
[React] redux module 예시 (0) 2021.07.06 [JavaScript] promise (0) 2021.07.04 [Python]내장함수 : replace (0) 2021.06.17 [Python] f 문자열 포매팅 (f-string formatting) (0) 2021.06.16 [Python] while (0) 2021.06.13