본문 바로가기
  • Hello_
Python/Error

[Python_Error] TypeError: '>' not supported between instances of 'list' and 'int'

by LDwDL 2022. 10. 1.
728x90
반응형

본 포스팅은 업무 중 발생했던 오류들을 예시를 통해 어떻게 해결을 하는지 작성했다.

 

TypeError: '>' not supported between instances of 'list' 
and 'int'

본 오류는 리스트 형태의 어레이를 관계 연산자('>', '<', '>=', '<=', '==', '!=')를 통해 하나의 정수 혹은 문자열로 비교하여 출력하고자 할 때 발생한다. 필자는 리스트 형태의 어레이를 np.where()의 입력으로 두고 관계 연산자의 조건을 주었을 때 발생했던 오류이다.

 

[예시]

nums = [1,2,3,4,5,6,7,8,9,10]
print(nums > 3)
"""
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      1 nums = [1,2,3,4,5,6,7,8,9,10]
----> 2 print(nums > 3)

TypeError: '>' not supported between instances of 'list' and 'int'
"""

 

[해결 방법]

 이는 단순히 리스트 형태의 어레이를 np.array를 통해 싸주고 넘파이 형태로 바꿔주면 해결이 가능하다.

nums = np.array([1,2,3,4,5,6,7,8,9,10])
print(nums > 3)
# [False False False  True  True  True  True  True  True  True]

 

혹은 같은 리스트 형태와 비교한다면 문제없이 작동된다.

nums = [1,2,3,4,5,6,7,8,9,10]
print(nums != [3])
# True

 

 

728x90
반응형

댓글