요즘 코딩 테스트를 풀면서 가끔씩 하는 실수가 생겼다.
그런데 매번 에러 메세지를 볼 때마다 뭐가 잘못 되었는지 몰라서 한참을 고민하고 소스를 들여다보곤 한다.
'저번에도 봤었던 에러 메세지였는데 그때 어디를 고쳤더라...?'의 무한굴레에 빠지곤 했다...
에러 메세지는 map 타입을 인덱싱할 수 없다는 뜻인데 매번 어느 부분이 문제였는지 살펴보느라 애를 먹었다.
TypeError: 'map' object is not subscriptable
아래 소스와 같이 입력 값을 리스트에 담으려고 했을 때 이와 같은 에러가 발생한다면...
n, m = map(int, input().split())
lst = []
for _ in range(n):
lst.append(map(int, input().split()))
if lst[0][0] >= 0:
print("yes")
map을 list로 변환해서 list에 넣었는지 확인해보자!
for _ in range(n):
lst.append(list(map(int, input().split())))
최종 정상 코드
n, m = map(int, input().split())
lst = []
for _ in range(n):
lst.append(list(map(int, input().split())))
if lst[0][0] >= 0:
print("yes")
* 잘못된 부분에 대해 댓글 남겨주시면 감사하겠습니다! 😀
'Python > Error' 카테고리의 다른 글
[Python/Error] AttributeError: 'str' object has no attribute 'capabilities' (0) | 2023.12.03 |
---|---|
[Python/Error] 'KeyedVectors' object has no attribute 'syn0' (0) | 2022.05.22 |