Python/Python numpy
Python numpy : tolist (array를 list로 변환. array list)
CosmosProject
2021. 12. 29. 00:58
728x90
반응형
numpy의 tolist method는 array를 python의 list로 바꿔줍니다.
변경할 때 array의 차원을 그대로 유지합니다.
2차원 array에 tolist를 적용시키면 list속에 다른 list가 있는 2차원 list의 형태로 반환해준다는 의미이죠.
아래 예시들을 봅시다.
import numpy as np
arr_test = np.array(
[1, 2, 3, 4, 5] # 1차 array
)
list_test = arr_test.tolist()
print(arr_test)
print(list_test)
-- Result
[1 2 3 4 5]
[1, 2, 3, 4, 5]
import numpy as np
arr_test = np.array(
[ # 1차 array
[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15] # 2차 array
]
)
list_test = arr_test.tolist()
print(arr_test)
print(list_test)
-- Result
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
import numpy as np
arr_test = np.array(
[ # 1차 array
[ # 2차 array
[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5] # 3차 array
],
[ # 2차 array
[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5] # 3차 array
],
]
)
list_test = arr_test.tolist()
print(arr_test)
print(list_test)
-- Result
[[[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]]
[[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]]]
[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]
728x90
반응형