Đã sắp xếp cmp python

Trong khoa học máy tính, thuật toán sắp xếp đặt các phần tử của danh sách vào một thứ tự cụ thể. Chúng quan trọng vì chúng thường làm giảm sự phức tạp của một vấn đề. Hãy cùng khám phá cách sử dụng các hàm sắp xếp tùy chỉnh để triển khai các thứ tự và so sánh tùy chỉnh trong Python

Trong bài viết trước về cách làm việc với các luồng trong Python, tôi đã giới thiệu sơ lược về phương pháp sắp xếp với danh sách. sắp xếp[] và sắp xếp[]. Cả

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
6 và
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 đều có một tham số chính chỉ định một hàm sẽ được gọi trên từng phần tử danh sách trước khi thực hiện so sánh

Trong bài viết này, tôi muốn đi xa hơn về chủ đề sắp xếp và khám phá cách viết hàm sắp xếp tùy chỉnh trong Python. Nói cách khác, tôi sẽ giải thích cách sử dụng hàm lambda tùy chỉnh làm tham số chính

Nếu bạn không cảm thấy thoải mái với các hàm Python, bạn nên đọc Cách xác định hàm trong Python trước khi tìm hiểu sâu hơn về bài viết này

Sắp xếp với chức năng sắp xếp tùy chỉnh trong Python

Đầu tiên, hãy nói về sự khác biệt giữa

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
1 và
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0. Về mặt cú pháp,
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
1 là một phương thức thể hiện được triển khai như
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
4, trong khi
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 được sử dụng như
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
6

Một điều quan trọng cần lưu ý là

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
1 trực tiếp sửa đổi biến ban đầu và do đó, thứ tự ban đầu sẽ bị mất

Mặt khác,

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 giữ một bản sao của biến ban đầu, giúp có thể trở lại thứ tự ban đầu nếu cần. Vì
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
1 không tạo bất kỳ bản sao nào của biến ban đầu nên nó hiệu quả hơn một chút so với
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0. Tuy nhiên, điều này phải trả giá bằng sự tiện lợi

Cũng cần lưu ý rằng

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 sẽ trả về một danh sách;

Còn đối với

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
6, nó sửa đổi danh sách tại chỗ và không có giá trị trả về. Cuối cùng nhưng không kém phần quan trọng,
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
6 chỉ có thể hoạt động trên danh sách trong khi
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 chấp nhận bất kỳ lần lặp nào

Ví dụ: đây là so sánh chuỗi không phân biệt chữ hoa chữ thường

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
3

Ghi chú. Người ta thường chuyển một hàm lambda tùy chỉnh làm tham số chính để sắp xếp các đối tượng phức tạp trong Python

Bây giờ, hãy nói về các hàm sắp xếp tùy chỉnh trong Python. Trong Python, chúng ta có thể viết các hàm sắp xếp tùy chỉnh hoạt động với

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
1 và
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0

Giá trị của tham số chính phải là một hàm nhận một đối số duy nhất và trả về một

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67 cho mục đích sắp xếp. Bởi vì chức năng chính chỉ được gọi một lần cho mỗi bản ghi đầu vào, đây là một cách hiệu quả để thực hiện sắp xếp trong Python

Một mẫu phổ biến là sắp xếp các đối tượng phức tạp bằng cách sử dụng một số chỉ số của đối tượng như

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. Ví dụ: chúng ta có thể xác định một thứ tự tùy chỉnh để sắp xếp danh sách các bộ dữ liệu

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]

Nó cũng hoạt động cho các đối tượng có thuộc tính tên

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
6

Bạn có thể tìm hiểu thêm về các đối tượng tùy chỉnh trong Python trong bài viết Các bước đơn giản để tạo lớp của riêng bạn trong Python

Biết cách thao tác dữ liệu, viết các hàm sắp xếp tùy chỉnh trong Python và thực hiện so sánh tùy chỉnh là những kỹ năng cần thiết để thành thạo. Giới thiệu về Python cho Khoa học dữ liệu của chúng tôi là một cách tuyệt vời để chọn bộ kỹ năng theo yêu cầu này

So sánh tùy chỉnh với chức năng sắp xếp trong Python

Bạn cũng có thể sử dụng

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 với bộ so sánh tùy chỉnh làm tham số của nó

Trong Python 2, có thể triển khai

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 bằng bộ so sánh tùy chỉnh, hoặc là tham số
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 hoặc tham số
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67

Điều quan trọng cần lưu ý là

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 cần truyền hai tham số [x và y] là một phần của danh sách. Nó sẽ trả về một số với logic sau

  • Nếu nó trả về một số dương. x > y
  • Nếu nó trả về 0. x == y
  • Nếu nó trả về một số âm. x < y

Tuy nhiên,

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67 nhận một tham số, tính toán kết quả và sau đó sử dụng tính toán để sắp xếp và so sánh. Điều này có nghĩa là trong Python 2, bạn có thể sắp xếp danh sách các số theo giá trị khối của chúng theo hai cách khác nhau

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
2

Trong Python 3, tham số

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 đã bị xóa, chủ yếu vì hai lý do

Đầu tiên, mọi thứ được thực hiện với

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 đều có thể được thực hiện với
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. Thứ hai,
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67 nhanh hơn
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21. Khi
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 được truyền dưới dạng tham số, thuật toán sắp xếp sẽ so sánh các cặp giá trị và hàm so sánh được gọi nhiều lần cho mỗi mục

Mặt khác, khóa chỉ thực hiện tính toán một lần. Như vậy độ phức tạp giảm đi. Điều này làm cho mã ít bị lỗi hơn vì cú pháp được đơn giản hóa. [Trước khóa, có thể hưởng lợi từ nó bằng cách tuân theo nguyên tắc Trang trí-Sắp xếp-Không trang trí, còn được gọi là biến đổi Schwartzian. ]

Nếu bạn đã quen thuộc với Java hoặc C++, bạn có thể quen thuộc với

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 hơn là
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. Trên thực tế, trong Python 3, bạn có thể sử dụng
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 với
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
64, điều này sẽ chuyển đổi
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 thành
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. Hãy cùng khám phá điều này nhiều hơn trong phần tiếp theo

Hàm sắp xếp tùy chỉnh trong Python với funcools. cmp_to_key[func]

công cụ chức năng. cmp_to_key[func] được sử dụng để chuyển đổi hàm so sánh kiểu cũ thành hàm chính. Nó có sẵn trong Python 2. 7, Trăn 3. 2, và sau này

Theo tài liệu Python 3, “hàm so sánh là bất kỳ hàm có thể gọi nào chấp nhận hai đối số, so sánh chúng và trả về một số âm cho giá trị nhỏ hơn, 0 cho giá trị bằng hoặc số dương cho giá trị lớn hơn. Hàm

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67 là một hàm có thể gọi được chấp nhận một đối số và trả về một giá trị khác được sử dụng làm sắp xếp
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. ”

Trước Python 2. 4, Không có sắp xếp [] và danh sách. sort[]  không có đối số từ khóa. Thay vào đó, Python 2 đã hỗ trợ tham số

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 để xử lý các hàm so sánh do người dùng chỉ định

Khi chuyển mã từ Python 2 sang Python 3, bạn có thể phải chuyển đổi hàm từ

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 thành
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. Trong Python 3,
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
64 đã được giới thiệu để tạo thuận lợi cho quá trình

Chúng tôi sẽ sử dụng

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
64 với các chức năng chấp nhận các chức năng chính như
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0 hoặc
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
75, mà tôi đã nói trong bài viết trước của mình. Sử dụng ví dụ trước của chúng tôi để sắp xếp các số theo giá trị khối của chúng, bạn có thể viết một hàm
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 tùy chỉnh như sau

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
6

Đôi khi, việc sử dụng phím có thể ít rõ ràng hơn

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21. Trong trường hợp này, có thể sử dụng
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
64 sẽ tốt hơn vì nó dễ đọc và trực quan hơn

Ví dụ, trong kỳ thi matura năm ngoái [một kỳ thi của Ba Lan tương tự như A Levels, Abitur hoặc Baccalauréat], phần CNTT tùy chọn có một bài tập bao gồm phần này

Cặp [số1, từ1] nhỏ hơn cặp [số2, từ2] nếu

  • số1 < số2

Hoặc

  • number1 == number2 và word1 nhỏ hơn theo thứ tự abc so với word2

Ví dụ: cặp [1, bbbb] nhỏ hơn cặp [2, aaa], nhưng cặp [3, aaa] nhỏ hơn cặp [3, ab]

Nói cách khác, chúng tôi muốn cặp được sắp xếp theo thứ tự tăng dần trên phần tử đầu tiên và phần tử thứ hai

Do đó, chúng tôi hy vọng các cặp sẽ được trả lại theo thứ tự sau. [1, bbbb], [2, aaa], [3, aaa], [3, ab]

Dưới đây là hàm

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 tùy chỉnh để giải quyết vấn đề này

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
7

Nhưng ngay cả trong trường hợp này, chúng ta có thể giải quyết vấn đề với

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67 bằng cách sắp xếp một danh sách các bộ

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
5

Chúng ta cũng có thể cố gắng làm cho vấn đề trở nên khó khăn hơn bằng cách sắp xếp phần tử đầu tiên theo thứ tự giảm dần và phần tử thứ hai theo thứ tự tăng dần. Một lần nữa, chúng ta có thể giải nó bằng

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
7

Giả sử chúng ta lật vấn đề theo cách khác, với phần tử đầu tiên theo thứ tự tăng dần và phần tử thứ hai theo thứ tự giảm dần. Trong trường hợp này, truyền tham số

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
52 thành
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
53 sẽ giải quyết được

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
0

Thật khó để tìm ra trường hợp mà

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 không thể thay thế bằng
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67. Bởi vì
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
64 về hiệu suất rất chậm so với
>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
67, nên nó chỉ nên được sử dụng như là phương sách cuối cùng để triển khai chức năng sắp xếp tùy chỉnh trong Python

Nếu bạn muốn biết thêm về các chức năng ánh xạ, hãy xem bài viết của tôi về filter[], map[] và reduce[]

Kết luận về các hàm sắp xếp tùy chỉnh trong Python

Trong bài viết này, chúng ta đã khám phá cách triển khai các hàm so sánh và sắp xếp tùy chỉnh trong Python. Chúng tôi đã tìm hiểu một chút về lịch sử Python và cố gắng hiểu các lựa chọn được thực hiện với

>>> pokemon = [
..    ['Charmander', 'Fire', 52],
..    ['Blastoise', 'Water', 83],
..    ['Beedrill', 'Poison', 90],
.. ]
>>> sorted[pokemon, key=lambda x: x[2]]   # sort by attack power
[['Charmander', 'Fire', 52],
 ['Blastoise', 'Water', 83],
 ['Beedrill', 'Poison', 90]]
21 và khóa giữa Python 2 và 3 để triển khai các hàm sắp xếp tùy chỉnh trong Python

Để hiểu rõ hơn về các khái niệm được giải thích trong các bài viết này, bạn nên chơi với các đoạn mã và xây dựng các ví dụ của riêng mình

Cuối cùng, nếu bạn muốn tìm hiểu thêm về thao tác dữ liệu trong Python, vui lòng xem bài viết xuất sắc của Yigit về Cách lọc hàng và chọn cột trong khung dữ liệu Python với gấu trúc

Và nếu bạn muốn đưa mọi thứ lên một tầm cao mới, hãy thử theo dõi Python cho Khoa học dữ liệu của chúng tôi. học tập vui vẻ

Chủ Đề