본문 바로가기

Error and Solve

[에러 해결] TypeError: only integer scalar arrays can be converted to a scalar index / np array로 바꾸기

반응형

 

 

에러: TypeError: only integer scalar arrays can be converted to a scalar index

TypeError: only integer scalar arrays can be converted to a scalar index

 

# Given lists
days = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
sorted_indices = np.array([10, 8, 17, 29, 24, 30, 22, 12, 25, 9])

days[sorted_indices]

 

 

integer로 구성된 np.array 배열을 가지고 인덱스 처리하려고 할 때 TypeError가 나타난다

 

 

 

 

해결: 리스트를 np.array로 만들기

 

days = np.array(days)

 

이용하는 리스트를 모두 np.array로 바꾸면 해결된다. 

 

 

 

 

 

 

 

 

코드

import numpy as np 
days = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
sorted_indices = np.array([10, 8, 17, 29, 24, 30, 22, 12, 25, 9])

days = np.array(days)
days[sorted_indices]
반응형