list.sort() # in-place

sorted(list)

 

sort()

built-in method인 list.sort()가 있으며 in-place로 리스트를 업데이트 한다

return None

original 리스트가 필요없을 때 사용하면 됨

리스트에만 사용 가능

a = [1,3,2,4]
print(sorted(a))
print(a)

# result
[1, 2, 3, 4]
[1, 3, 2, 4]

 

sorted()

built-in function으로 iterable인 새로운 정렬된 리스트를 만든다

a = [1,3,2,4]
a.sort()
print(a)

# result
[1, 2, 3, 4]

 

key parameter를 가짐

sorted( list, key=func, reverse=bool)

list.sort(key=func, reverse-bool)

 

# 연습

A = [1,3,3,5,5,7,8,6,5,4,2]
B = list(set(A))
B.sort(reverse=True)
B

A = [1,3,3,5,5,7,8,6,5,4,2]
sorted(list(set(A)),reverse=True)

 

https://docs.python.org/3/howto/sorting.html

 

Sorting HOW TO — Python 3.10.0 documentation

Sorting HOW TO Author Andrew Dalke and Raymond Hettinger Release 0.1 Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable. In this documen

docs.python.org

https://infinitt.tistory.com/122

 

파이썬 (Python) - (정렬 총정리) sort( ), sorted( ) , 특정 key를 기준으로 정렬하기 ,이중 리스트 정렬

sorted() Prototype sorted( , key = , reverse = ) # 뿐 아니라, , , 에도 사용 가능하다. 원본 내용을 바꾸지 않고, 정렬한 값을 반환한다. List, tuple, Dictionary, str에 모두 사용 가능하다. key 를 통하여..

infinitt.tistory.com

https://kingofbackend.tistory.com/98

 

[Python] sort()에서의 key lambda 사용하기

파이썬의 sort() 함수는 오름차순으로 정렬해주는 함수입니다. 문자열 또한 오름차순으로 정렬이 됩니다. sort(reverse=True) 또는 reverse()를 이용하여 내림차순으로도 구현이 가능합니다. 이 글에서는

kingofbackend.tistory.com

 

+ Recent posts