알고리즘 문제를 풀다가
arr = list(map(int, input().split()))
라는 코드가 있었다. 정말 자연스럽게 주어진 코드였는데 문득,
arr = [map(int, input().split())]
로 코드를 작성하는 게 더 간이할 것 같은데? 라는 생각이 들었다.
a, b = map(int, input().split())
정말 많이 쓰는 코드니까 당연히 되겠지 싶었는데 []는 안된다.
map()은 주어진 코드에 따라int 형변환을 하려고 대기 중인 map 객체이다. 여기서 포인트는 대기 중인 map 객체라는 것이다.
a, b = map(int, input().split())
을 보면 대기 중인 map 객체를 a, b로 unpacking을 하면서 값을 할당한 걸 볼 수 있는데
내가 작성한 리스트 리터럴 형식의 문법은 안에서 대기 중인 값들을 unpacking할 방법이 없다. 따라서 arr에 리스트로 씌워진 map 객체 하나만 달랑 있는 것이다.
그럼 여기서 생각해볼만한 부분이 그럼 *로 unpacking 하면 되겠네?
arr = [map(int, input().split())]
print('[map(int,input().split())]', type(arr))
print(type(arr[0]))
arr_with_unpacking =[*map(int, input().split())]
print('*map(int, input().split())', arr_with_unpacking)
print(type(arr_with_unpacking[0]))

잘 되는 걸 볼 수 있다. 문득 생각이 나서 해봤는데, 좋은 접근법이었다.