본문 바로가기

python key를 이용한 단어 정렬 (등장하는 단어순)

2020. 6. 16.
str = 'asdfasdfasdfasdf'

print(str)
print(sorted(str, key=str.find))

처음 등장한 문자 순서대로 정렬 하고 싶을 때 str.find를 key값으로 설정 하면 된다.

 

출력 결과

asdfasdfasdfasdf
['a', 'a', 'a', 'a', 's', 's', 's', 's', 'd', 'd', 'd', 'd', 'f', 'f', 'f', 'f']

 

'Python' 카테고리의 다른 글

python 숫자, 문자열로 list 만들기  (0) 2020.06.16
(python) itertools의 permutations이용하기  (0) 2020.06.16
python [::] 사용법  (0) 2020.06.15
python 아스키코드 이용하기  (0) 2020.06.15
python list, max 공부  (0) 2020.06.12
댓글