해결된 질문
작성
·
257
답변 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)