파이썬 정렬 목록

Python에는 목록을 정렬하는 두 가지 방법이 있습니다. 하나 는 반환 값이 없는 sort()
메서드를 호출하여 목록 자체를 오름차순으로 정렬하는 것입니다.

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)

산출:

['audi', 'bmw', 'subaru', 'toyota']

또 다른 방법은 원래 목록에 영향을 주지 않고 오름차순으로 정렬된 목록을 반환하는 sorted() 함수를 사용하는 것입니다 .

cars = ['bmw', 'audi', 'toyota', 'subaru']

print("Here is the original list:")
print(cars)

print("\nHere is the sorted list:")
print(sorted(cars))

print("\nHere is the original list again:")
print(cars)

산출:

Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']

참조: Eric Mathers의 "Python Programming from Introduction to Practice(2판)" Yuan Guozhong 번역

추천

출처blog.csdn.net/m0_59838087/article/details/120680971