반응형
sorted, 문자열 길이로 정렬, 한글 정렬
the_list.sort() # sorts normally by alphabetical order
the_list.sort(key=len, reverse=True) # sorts by descending length
the_list.sort(key=lambda item: (-len(item), item))
#########################################
n = ['aaa', 'bbb', 'ccc', 'dddd', 'dddl', 'yyyyy']
for i in reversed(sorted(n, key=len)):
print i
for i in sorted(n, key=len, reverse=True):
print i
-Sort your list by alpha order, then by length.
See the following exmple:
>>> coursesList = ["chemistry","physics","mathematics","art"]
>>> sorted(coursesList,key=len)
['art', 'physics', 'chemistry', 'mathematics']
>>> coursesList.append("mopsosa")
>>> sorted(coursesList,key=len)
['art', 'physics', 'mopsosa', 'chemistry', 'mathematics']
>>> coursesList.sort()
>>> sorted(coursesList,key=len)
['art', 'mopsosa', 'physics', 'chemistry', 'mathematics']
반응형
'프로그래밍 > Python' 카테고리의 다른 글
[python] style cloud 설정 (0) | 2020.12.15 |
---|---|
[python] wordcloud시 불용어 지정 (0) | 2020.12.15 |
[python] 큰 파일 분할해서 만들기 (0) | 2020.12.14 |
[python] konlpy - Okt, komoran, Pykomoran (0) | 2020.12.14 |
[python] Customized Konlpy 사용하기 (0) | 2020.12.09 |