강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

남기정님의 프로필 이미지
남기정

작성한 질문수

실리콘밸리 엔지니어가 가르치는 파이썬 기초부터 고급까지

Map / Filter function(맵과 필터 함수)에 대해서 알아보기

map,filter

해결된 질문

작성

·

257

2

map과 filter값을 받아오기위해서는

방법이

  1. for루프

  2. list()함수

이 방법밖에 없는건가요??

그리고 map,filter인자에는 list만 들어갈수있나요?? 딕셔너리,set, 튜플등은 안되나요?

답변 1

0

미쿡엔지니어님의 프로필 이미지
미쿡엔지니어
지식공유자

안녕하세요 남기정님,

아래의 예제는 Lambda를 사용해서 쓸수 있는 방법에 대해 보실 수 있습니다.

# Sample list of integers
numbers = [1, 2, 3, 4, 5]

# Using map with a lambda function to square each element
squared_numbers = map(lambda x: x**2, numbers)

# Convert the map object to a list to see the results
result = list(squared_numbers)

# Print the squared numbers
print(result)

또한 Dictionary나 Set도 사용 가능합니다.

# Sample dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}

# Using map with a lambda function to square each value in the dictionary
squared_keys = map(lambda key: my_dict[key]**2, my_dict)

# Convert the map object to a list to see the results
result = list(squared_keys)

# Print the squared keys
print(result)
남기정님의 프로필 이미지
남기정

작성한 질문수

질문하기