[Python Snippets] Python map

Python map

map과 lambda의 활용

# 주어진 문자열을 각 단어 별로 역순으로 하여서,출력 하라.

words = 'abc 123 def xyz'.split(' ')
print(''.join(map(lambda word: word[::-1], words)))

>> 'cba 321 fed zyx'

map과 range의 활용

# 5 ~ 1 까지 역순으로 수를 만드는데, 타입을 str로 하는 리스트를 생성

In [9]: list(map(str, range(5, 0, -1)))
Out[9]: ['5', '4', '3', '2', '1']