排序是编程中非常常见的一个操作,Python 提供了多种方法来对列表进行排序。以下是一些常用的排序方法,共计58种,帮助你轻松掌握Python中的高效排序技巧。1. 使用内置的 sorted() 函数...
排序是编程中非常常见的一个操作,Python 提供了多种方法来对列表进行排序。以下是一些常用的排序方法,共计58种,帮助你轻松掌握Python中的高效排序技巧。
sorted() 函数Python 的内置函数 sorted() 可以对任何可迭代的对象进行排序,返回一个新的列表。
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = sorted(my_list)sort() 方法列表的 sort() 方法可以就地修改列表,即不需要创建新的列表。
my_list.sort()sorted() 和 sort() 方法都允许你传递一个 key 参数,该参数是一个函数,用于定义排序的依据。
my_list = [('apple', 2), ('banana', 1), ('cherry', 3)]
sorted_list = sorted(my_list, key=lambda x: x[1])reverse 参数sorted() 和 sort() 方法都有一个 reverse 参数,用于指定排序的顺序。
sorted_list = sorted(my_list, reverse=True)sort() 方法的 reverse 参数my_list.sort(reverse=True)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)))bisect 模块bisect 模块提供了对有序列表进行二分查找和插入的方法。
import bisect
my_list = [1, 3, 4, 7, 10]
bisect.insort(my_list, 5)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)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))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)operator 模块operator 模块提供了一些常用的操作符作为函数。
from operator import itemgetter
my_list = [('apple', 2), ('banana', 1), ('cherry', 3)]
sorted_list = sorted(my_list, key=itemgetter(1))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)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])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))]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])))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)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)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))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)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)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])))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)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 lstarray 模块进行选择排序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 lstbisect 模块进行归并排序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 resultheapq 模块进行堆排序import heapq
def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))]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])))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)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 lstarray 模块进行选择排序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 lstbisect 模块进行归并排序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 resultheapq 模块进行堆排序import heapq
def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))]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])))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)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 lstarray 模块进行选择排序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 lstbisect 模块进行归并排序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 resultheapq 模块进行堆排序import heapq
def heap_sort(lst): heapq.heapify(lst) return [heapq.heappop(lst) for _ in range(len(lst))]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])))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)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