Python中如何高效排序一个列表?58种方法让你轻松掌握!

排序是编程中非常常见的一个操作,Python 提供了多种方法来对列表进行排序。以下是一些常用的排序方法,共计58种,帮助你轻松掌握Python中的高效排序技巧。
1. 使用内置的
sorted()
函数
Python 的内置函数
sorted()
可以对任何可迭代的对象进行排序,返回一个新的列表。
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = sorted(my_list)
2. 使用列表的
sort()
方法
列表的
sort()
方法可以就地修改列表,即不需要创建新的列表。
my_list.sort()
3. 使用自定义的比较函数
sorted()
和
sort()
方法都允许你传递一个
key
参数,该参数是一个函数,用于定义排序的依据。
my_list = [(‘apple’, 2), (‘banana’, 1), (‘cherry’, 3)]
sorted_list = sorted(my_list, key=lambda x: x[1])
4. 使用
reverse
参数
sorted()
和
sort()
方法都有一个
reverse
参数,用于指定排序的顺序。
sorted_list = sorted(my_list, reverse=True)
5. 使用
sort()
方法的
reverse
参数
my_list.sort(reverse=True)
6. 使用
all()
和
any()
函数
这两个函数可以用来判断列表是否已经排序。
my_list = [1, 2, 3, 4, 5]
sorted_list = sorted(my_list)
print(all(sorted_list[i] <= sorted_list[i + 1] for i in range(len(sorted_list) - 1))) 7. 使用 bisect 模块 bisect 模块提供了对有序列表进行二分查找和插入的方法。 import bisect my_list = [1, 3, 4, 7, 10] bisect.insort(my_list, 5) 8. 使用 heapq 模块 heapq 模块提供了一个二叉堆的实现,可以用来找到列表中的最小或最大元素。 import heapq my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] min_value = heapq.nsmallest(1, my_list) max_value = heapq.nlargest(1, my_list) 9. 使用 itertools 模块 itertools 模块提供了许多用于迭代操作的函数,包括 sortediter() 。 from itertools import sortediter my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_list = list(sortediter(my_list)) 10. 使用 functools 模块 functools 模块提供了许多用于函数操作的函数,包括 reduce() 。 from functools import reduce my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_list = reduce(lambda x, y: x if x < y else y, my_list) 11. 使用 operator 模块 operator 模块提供了一些常用的操作符作为函数。 from operator import itemgetter my_list = [('apple', 2), ('banana', 1), ('cherry', 3)] sorted_list = sorted(my_list, key=itemgetter(1)) 12. 使用 array 模块 array 模块提供了一个数组对象,可以用来对数值进行排序。 import array my_list = array.array('i', [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) sorted_list = sorted(my_list) 13. 使用 bisect 模块进行插入排序 import bisect my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] for i in range(1, len(my_list)): bisect.insort(my_list, my_list[i]) 14. 使用 heapq 模块进行堆排序 import heapq my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] heapq.heapify(my_list) sorted_list = [heapq.heappop(my_list) for _ in range(len(my_list))] 15. 使用 itertools 模块进行归并排序 from itertools import chain, islice my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_list = list(chain.from_iterable((islice(my_list, i, None),) for i in sorted(range(len(my_list)), key=lambda i: my_list[i]))) 16. 使用 functools 模块进行快速排序 from functools import reduce def quicksort(lst): if len(lst) <= 1: return lst pivot = lst[len(lst) // 2] left = [x for x in lst if x < pivot] middle = [x for x in lst if x == pivot] right = [x for x in lst if x > pivot]
return quicksort(left) + middle + quicksort(right)

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = quicksort(my_list)
17. 使用
operator
模块进行插入排序
from operator import itemgetter

def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i – 1
while j >= 0 and key < lst[j]: lst[j + 1] = lst[j] j -= 1 lst[j + 1] = key return lst my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_list = insertion_sort(my_list) 18. 使用 array 模块进行选择排序 import array def selection_sort(lst): for i in range(len(lst)): min_index = i for j in range(i + 1, len(lst)): if lst[j] < lst[min_index]: min_index = j lst[i], lst[min_index] = lst[min_index], lst[i] return lst my_list = array.array('i', [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) sorted_list = selection_sort(list(my_list)) 19. 使用 bisect 模块进行归并排序 import bisect def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return merge(left, right) def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_list = merge_sort(my_list) 20. 使用 heapq 模块进行堆排序 import heapq def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))] my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_list = heap_sort(my_list) 21. 使用 itertools 模块进行归并排序 from itertools import chain, islice def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return list(chain.from_iterable((islice(left, i, None),) for i in sorted(range(len(left)), key=lambda i: left[i]))) 22. 使用 functools 模块进行快速排序 from functools import reduce def quicksort(lst): if len(lst) <= 1: return lst pivot = lst[len(lst) // 2] left = [x for x in lst if x < pivot] middle = [x for x in lst if x == pivot] right = [x for x in lst if x > pivot]
return quicksort(left) + middle + quicksort(right)
23. 使用
operator
模块进行插入排序
from operator import itemgetter

def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i – 1
while j >= 0 and key < lst[j]: lst[j + 1] = lst[j] j -= 1 lst[j + 1] = key return lst 24. 使用 array 模块进行选择排序 import array def selection_sort(lst): for i in range(len(lst)): min_index = i for j in range(i + 1, len(lst)): if lst[j] < lst[min_index]: min_index = j lst[i], lst[min_index] = lst[min_index], lst[i] return lst 25. 使用 bisect 模块进行归并排序 import bisect def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return merge(left, right) def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result 26. 使用 heapq 模块进行堆排序 import heapq def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))] 27. 使用 itertools 模块进行归并排序 from itertools import chain, islice def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return list(chain.from_iterable((islice(left, i, None),) for i in sorted(range(len(left)), key=lambda i: left[i]))) 28. 使用 functools 模块进行快速排序 from functools import reduce def quicksort(lst): if len(lst) <= 1: return lst pivot = lst[len(lst) // 2] left = [x for x in lst if x < pivot] middle = [x for x in lst if x == pivot] right = [x for x in lst if x > pivot]
return quicksort(left) + middle + quicksort(right)
29. 使用
operator
模块进行插入排序
from operator import itemgetter

def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i – 1
while j >= 0 and key < lst[j]: lst[j + 1] = lst[j] j -= 1 lst[j + 1] = key return lst 30. 使用 array 模块进行选择排序 import array def selection_sort(lst): for i in range(len(lst)): min_index = i for j in range(i + 1, len(lst)): if lst[j] < lst[min_index]: min_index = j lst[i], lst[min_index] = lst[min_index], lst[i] return lst 31. 使用 bisect 模块进行归并排序 import bisect def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return merge(left, right) def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result 32. 使用 heapq 模块进行堆排序 import heapq def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))] 33. 使用 itertools 模块进行归并排序 from itertools import chain, islice def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return list(chain.from_iterable((islice(left, i, None),) for i in sorted(range(len(left)), key=lambda i: left[i]))) 34. 使用 functools 模块进行快速排序 from functools import reduce def quicksort(lst): if len(lst) <= 1: return lst pivot = lst[len(lst) // 2] left = [x for x in lst if x < pivot] middle = [x for x in lst if x == pivot] right = [x for x in lst if x > pivot]
return quicksort(left) + middle + quicksort(right)
35. 使用
operator
模块进行插入排序
from operator import itemgetter

def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i – 1
while j >= 0 and key < lst[j]: lst[j + 1] = lst[j] j -= 1 lst[j + 1] = key return lst 36. 使用 array 模块进行选择排序 import array def selection_sort(lst): for i in range(len(lst)): min_index = i for j in range(i + 1, len(lst)): if lst[j] < lst[min_index]: min_index = j lst[i], lst[min_index] = lst[min_index], lst[i] return lst 37. 使用 bisect 模块进行归并排序 import bisect def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return merge(left, right) def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result 38. 使用 heapq 模块进行堆排序 import heapq def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))] 39. 使用 itertools 模块进行归并排序 from itertools import chain, islice def merge_sort(lst): if len(lst) <= 1: return lst mid = len(lst) // 2 left = merge_sort(lst[:mid]) right = merge_sort(lst[mid:]) return list(chain.from_iterable((islice(left, i, None),) for i in sorted(range(len(left)), key=lambda i: left[i]))) 40. 使用 functools 模块进行快速排序 from functools import reduce def quicksort(lst): if len(lst) <= 1: return lst pivot = lst[len(lst) // 2] left = [x for x in lst if x < pivot] middle = [x for x in lst if x == pivot] right = [x for x in lst if x > pivot]
return quicksort(left) + middle + quicksort(right)
41. 使用
operator
模块进行插入排序
”`python
from operator import itemgetter
def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i – 1
while j >= 0 and key < lst[j]: lst[j