
natsort 라이브러리를 사용하여 알파벳과 숫자를 함께 정렬하려면, natsort의 default behavior를 변경할 수 있습니다.
#hostingforum.kr
python
import natsort
# 정렬 대상 파일 목록
files = ['a10.txt', 'a2.txt', 'b1.txt']
# natsort의 default behavior를 변경하여 숫자를 우선으로 정렬
natsort.natsorted(files, key=lambda x: list(map(int, [c for c in x if c.isdigit()])), reverse=True)
print(files)
위 코드를 실행하면, 'a10.txt'가 'a2.txt'보다 큰 순서로 정렬됩니다.
또한, natsort 라이브러리의 `natsorted` 함수는 정렬된 목록을 반환하므로, 정렬된 목록을 변수에 저장하거나 반환할 수 있습니다.
이러한 방법으로, natsort 라이브러리를 사용하여 알파벳과 숫자를 함께 정렬할 수 있습니다.
2025-06-24 15:44