Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Sắp xếp dữ liệu bằng Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Sorting Data With Python
Tất cả các lập trình viên sẽ phải viết mã để sắp xếp các mục hoặc dữ liệu tại một số điểm. Việc sắp xếp có thể rất quan trọng đối với trải nghiệm người dùng trong ứng dụng của bạn, cho dù đó là việc đặt hàng một hoạt động gần đây nhất của người dùng bằng dấu thời gian hay đặt danh sách người nhận email theo thứ tự bảng chữ cái theo tên cuối cùng. Chức năng phân loại Python cung cấp các tính năng mạnh mẽ để sắp xếp cơ bản hoặc tùy chỉnh thứ tự ở mức hạt.
Trong hướng dẫn này, bạn sẽ học cách sắp xếp các loại dữ liệu khác nhau trong các cấu trúc dữ liệu khác nhau, tùy chỉnh thứ tự và làm việc với hai phương thức sắp xếp khác nhau trong Python.
Đến cuối hướng dẫn này, bạn sẽ biết cách:
Thực hiện phân loại và đặt hàng Python cơ bản trên các cấu trúc dữ liệu
Tùy chỉnh một thứ tự sắp xếp phức tạp trong mã của bạn dựa trên các yêu cầu duy nhất
Đối với hướng dẫn này, bạn sẽ cần một sự hiểu biết cơ bản về danh sách và bộ dữ liệu cũng như các bộ. Những cấu trúc dữ liệu đó sẽ được sử dụng trong hướng dẫn này và một số hoạt động cơ bản sẽ được thực hiện trên chúng. Ngoài ra, hướng dẫn này sử dụng Python 3, vì vậy đầu ra ví dụ trong hướng dẫn này có thể thay đổi một chút nếu bạn sử dụng Python 2.
Đặt hàng các giá trị với >>> numbers_tuple = (6, 9, 3, 1)
>>> numbers_set = {5, 5, 10, 1, 0}
>>> numbers_tuple_sorted = sorted(numbers_tuple)
>>> numbers_set_sorted = sorted(numbers_set)
>>> numbers_tuple_sorted
[1, 3, 6, 9]
>>> numbers_set_sorted
[0, 1, 5, 10]
4
Để bắt đầu với việc sắp xếp Python, trước tiên bạn sẽ xem cách sắp xếp cả dữ liệu số và dữ liệu chuỗi.
Sắp xếp số
Bạn có thể sử dụng Python để sắp xếp danh sách bằng cách sử dụng
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
Đầu ra từ mã này là một danh sách mới, được sắp xếp. Khi biến ban đầu được in, các giá trị ban đầu không thay đổi.
>>> string_value='I like to sort'>>> sorted_string=sorted(string_value.split())>>> sorted_string['I', 'like', 'sort', 'to']>>> ' '.join(sorted_string)'I like sort to'
Đầu ra từ mã này là một danh sách mới, được sắp xếp. Khi biến ban đầu được in, các giá trị ban đầu không thay đổi.
Ví dụ này cho thấy bốn đặc điểm quan trọng của >>> numbers_tuple = (6, 9, 3, 1)
>>> numbers_set = {5, 5, 10, 1, 0}
>>> numbers_tuple_sorted = sorted(numbers_tuple)
>>> numbers_set_sorted = sorted(numbers_set)
>>> numbers_tuple_sorted
[1, 3, 6, 9]
>>> numbers_set_sorted
[0, 1, 5, 10]
4:
4 không phải được xác định. Nó có một chức năng tích hợp có sẵn trong một bản cài đặt Python tiêu chuẩn.
>>> numbers_tuple = (6, 9, 3, 1)
>>> numbers_set = {5, 5, 10, 1, 0}
>>> numbers_tuple_sorted = sorted(numbers_tuple)
>>> numbers_set_sorted = sorted(numbers_set)
>>> numbers_tuple_sorted
[1, 3, 6, 9]
>>> numbers_set_sorted
[0, 1, 5, 10]
4, không có đối số hoặc tham số bổ sung, đang đặt hàng các giá trị trong >>> numbers_tuple = (6, 9, 3, 1)
>>> numbers_set = {5, 5, 10, 1, 0}
>>> numbers_tuple_sorted = sorted(numbers_tuple)
>>> numbers_set_sorted = sorted(numbers_set)
>>> numbers_tuple_sorted
[1, 3, 6, 9]
>>> numbers_set_sorted
[0, 1, 5, 10]
9 theo thứ tự tăng dần, có nghĩa là nhỏ nhất đến lớn nhất.
4 cung cấp đầu ra được sắp xếp và không thay đổi giá trị ban đầu tại chỗ.
>>>
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
Lỗi này cho thấy lý do tại sao Python có thể sắp xếp các giá trị được đưa ra cho nó. Nó cố gắng đặt các giá trị theo thứ tự bằng cách sử dụng ít hơn toán tử (
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
5) để xác định giá trị nào thấp hơn theo thứ tự sắp xếp. Bạn có thể sao chép lỗi này bằng cách so sánh thủ công hai giá trị:
>>>
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
Cùng
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
6 được ném khi bạn cố gắng so sánh hai giá trị không thể so sánh mà không cần sử dụng
Nếu các giá trị trong danh sách có thể được so sánh và sẽ không ném
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
6, thì danh sách có thể được sắp xếp. Điều này ngăn chặn việc sắp xếp các vòng lặp với các giá trị không thể đặt hàng về bản chất và tạo ra đầu ra có thể không có ý nghĩa.
Ví dụ: số
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
9 có nên đến trước từ
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
0 không? Tuy nhiên, nếu một điều khác có chứa sự kết hợp của các số nguyên và chuỗi là tất cả các số, chúng có thể được chuyển theo các loại dữ liệu có thể so sánh bằng cách sử dụng danh sách hiểu biết:
>>>
>>> mixed_numbers=[5,"1",100,"34"]>>> sorted(mixed_numbers)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'str' and 'int'>>> # List comprehension to convert all values to integers>>> [int(x)forxinmixed_numbers][5, 1, 100, 34]>>> sorted([int(x)forxinmixed_numbers])[1, 5, 34, 100]
Mỗi phần tử trong
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
1 có
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
4 sau đó được gọi và có thể so sánh thành công từng phần tử và cung cấp đầu ra được sắp xếp.
Python cũng có thể ngầm chuyển đổi một giá trị sang loại khác. Trong ví dụ dưới đây, việc đánh giá
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
6 là một tuyên bố sai, do đó, đầu ra của đánh giá sẽ là
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
7. Số
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
9 có thể được chuyển đổi thành
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
9 dưới dạng
>>> mixed_numbers=[5,"1",100,"34"]>>> sorted(mixed_numbers)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'str' and 'int'>>> # List comprehension to convert all values to integers>>> [int(x)forxinmixed_numbers][5, 1, 100, 34]>>> sorted([int(x)forxinmixed_numbers])[1, 5, 34, 100]
0, trong khi
>>> mixed_numbers=[5,"1",100,"34"]>>> sorted(mixed_numbers)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'str' and 'int'>>> # List comprehension to convert all values to integers>>> [int(x)forxinmixed_numbers][5, 1, 100, 34]>>> sorted([int(x)forxinmixed_numbers])[1, 5, 34, 100]
1 chuyển đổi thành
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
7.
Mặc dù các yếu tố trong danh sách trông khác nhau, tất cả chúng đều có thể được chuyển đổi thành booleans (
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
9 hoặc
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
>>> mixed_numbers=[5,"1",100,"34"]>>> sorted(mixed_numbers)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'str' and 'int'>>> # List comprehension to convert all values to integers>>> [int(x)forxinmixed_numbers][5, 1, 100, 34]>>> sorted([int(x)forxinmixed_numbers])[1, 5, 34, 100]
6 và
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
6 được chuyển đổi thành
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
7 và được trả về trong đầu ra đã đặt hàng.
Ví dụ này minh họa một khía cạnh quan trọng của việc sắp xếp: Sắp xếp sự ổn định. Trong Python, khi bạn sắp xếp các giá trị bằng nhau, chúng sẽ giữ lại thứ tự ban đầu của mình theo đầu ra. Mặc dù
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
9 đã di chuyển, tất cả các giá trị khác đều bằng nhau để chúng giữ lại thứ tự ban đầu của chúng so với nhau. Trong ví dụ dưới đây, tất cả các giá trị được coi là bằng nhau và sẽ giữ lại các vị trí ban đầu của chúng:sort stability. In Python, when you sort equal values, they will retain their
original order in the output. Even though the
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
9 moved, all the other values are equal so they retain their original order relative to each other. In the example below, all the values are considered equal and will retain their original positions:
4 sẽ so sánh các chuỗi và vì năm ký tự đầu tiên giống nhau, đầu ra sẽ dựa trên ký tự thứ sáu.
Các chuỗi có chứa các giá trị giống hệt nhau sẽ kết thúc ngắn nhất đến dài nhất do các chuỗi ngắn hơn không có các phần tử để so sánh với các chuỗi dài hơn:
2. Đối số này hy vọng một hàm sẽ được truyền cho nó và hàm đó sẽ được sử dụng trên mỗi giá trị trong danh sách được sắp xếp để xác định thứ tự kết quả.
Để chứng minh một ví dụ cơ bản, hãy để giả sử yêu cầu đặt hàng một danh sách cụ thể là độ dài của các chuỗi trong danh sách, ngắn nhất đến dài nhất. Hàm trả về độ dài của chuỗi,
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
0
Thứ tự kết quả là một danh sách với thứ tự chuỗi ngắn nhất đến dài nhất. Độ dài của mỗi phần tử trong danh sách được xác định bởi
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
1
Thứ tự kết quả là một danh sách với thứ tự chuỗi ngắn nhất đến dài nhất. Độ dài của mỗi phần tử trong danh sách được xác định bởi
2 đang được gọi trên mỗi phần tử để xác định thứ tự sắp xếp, nhưng các giá trị ban đầu sẽ ở đầu ra.
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
2
Có hai hạn chế chính khi bạn sử dụng các chức năng với đối số
2 phải có khả năng xử lý tất cả các giá trị trong ITBERBELLE. Ví dụ: bạn có một danh sách các số được biểu diễn dưới dạng các chuỗi sẽ được sử dụng trong
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
4. Nếu một giá trị trong ITEBLE có thể được chuyển thành một số nguyên, thì hàm sẽ thất bại:
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
2 cực kỳ mạnh mẽ vì hầu hết mọi chức năng, có thể định nghĩa tích hợp hoặc do người dùng xác định, có thể được sử dụng để thao tác thứ tự đầu ra.
Nếu yêu cầu đặt hàng là đặt hàng có thể lặp lại bằng chữ cái cuối cùng trong mỗi chuỗi (và nếu chữ cái giống nhau, thì hãy sử dụng chữ cái tiếp theo), thì một hàm có thể được xác định và sau đó được sử dụng trong việc sắp xếp. Ví dụ dưới đây xác định một hàm đảo ngược chuỗi được truyền cho nó và sau đó hàm đó được sử dụng làm đối số cho
59 được áp dụng cho nó và thứ tự sắp xếp sẽ dựa trên các ký tự trong từ ngược.
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
4
Phải được định nghĩa nội tuyến
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
5
Không có tên
Có thể chứa các tuyên bố
Sẽ thực thi chỉ giống như một hàm
Trong ví dụ dưới đây, >>> string_number_value = '34521'
>>> string_value = 'I like to sort'
>>> sorted_string_number = sorted(string_number_value)
>>> sorted_string = sorted(string_value)
>>> sorted_string_number
['1', '2', '3', '4', '5']
>>> sorted_string
[' ', ' ', ' ', 'I', 'e', 'i', 'k', 'l', 'o', 'o', 'r', 's', 't', 't']
2 được định nghĩa là >>> numbers = [6, 9, 3, 1]
>>> numbers_sorted = sorted(numbers)
>>> numbers_sorted
[1, 3, 6, 9]
>>> numbers
[6, 9, 3, 1]
60 không có tên, đối số được thực hiện bởi >>> numbers = [6, 9, 3, 1]
>>> numbers_sorted = sorted(numbers)
>>> numbers_sorted
[1, 3, 6, 9]
>>> numbers
[6, 9, 3, 1]
60 là >>> numbers = [6, 9, 3, 1]
>>> numbers_sorted = sorted(numbers)
>>> numbers_sorted
[1, 3, 6, 9]
>>> numbers
[6, 9, 3, 1]
66 và >>> numbers = [6, 9, 3, 1]
>>> numbers_sorted = sorted(numbers)
>>> numbers_sorted
[1, 3, 6, 9]
>>> numbers
[6, 9, 3, 1]
67 là thao tác sẽ được thực hiện trên đối số:
67 được gọi trên mỗi phần tử và đảo ngược từ. Đầu ra đảo ngược đó sau đó được sử dụng để sắp xếp, nhưng các từ gốc vẫn được trả về.
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
3 và sửa đổi các giá trị tại chỗ. Hãy cùng xem các tác động của cả hai sự khác biệt trong mã:
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
5, vì vậy việc gán cho một biến mới chỉ truyền một loại
>>> mixed_types=[None,0]>>> sorted(mixed_types)Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'int' and 'NoneType'
3.
Danh sách
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
02 đã được thay đổi tại chỗ và thứ tự ban đầu không được duy trì theo bất kỳ cách nào.
4. Tại đây, bạn có thể sắp xếp một danh sách các cụm từ theo chữ cái thứ hai của từ thứ ba và trả lại danh sách ngược lại:
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
Hãy nói rằng có một cuộc đua 5K sắp diễn ra: Python 5K hàng năm đầu tiên. Dữ liệu từ cuộc đua cần được ghi lại và sắp xếp. Dữ liệu cần được ghi lại là số BIB của Runner và số giây cần có để hoàn thành cuộc đua:
>>>
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
9
Khi các vận động viên vượt qua vạch đích, mỗi
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
14 sẽ được thêm vào một danh sách gọi là
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
15. Trong các cuộc đua 5K, không phải tất cả các vận động viên vượt qua vạch xuất phát cùng một lúc, vì vậy người đầu tiên vượt qua vạch đích có thể không thực sự là người nhanh nhất:
Mỗi lần một người chạy băng qua vạch đích, số bib của họ và tổng thời lượng của chúng tính bằng giây được thêm vào
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
15.
Giờ đây, lập trình viên nghiêm túc phụ trách xử lý dữ liệu kết quả cho thấy danh sách này, biết rằng 5 người tham gia nhanh nhất là người chiến thắng nhận được giải thưởng và các vận động viên còn lại sẽ được sắp xếp nhanh nhất.
Không có yêu cầu cho nhiều loại sắp xếp theo các thuộc tính khác nhau. Danh sách là một kích thước hợp lý. Không có đề cập đến việc lưu trữ danh sách ở đâu đó. Chỉ sắp xếp theo thời gian và lấy năm người tham gia với thời gian thấp nhất:
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
19 từ mỗi người chạy và sắp xếp
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
15 được sắp xếp, 5 yếu tố đầu tiên được lưu trữ trong
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
23.
Nhiệm vụ đã hoàn thành! Giám đốc cuộc đua đến và thông báo cho lập trình viên rằng kể từ khi phát hành Python hiện tại là 3,7, họ đã quyết định rằng mỗi người thứ ba mươi bảy đã vượt qua vạch đích sẽ nhận được một túi tập thể dục miễn phí.
Tại thời điểm này, lập trình viên bắt đầu đổ mồ hôi vì danh sách các vận động viên đã thay đổi không thể đảo ngược. Không có cách nào để phục hồi danh sách ban đầu của các vận động viên theo thứ tự họ đã hoàn thành và tìm thấy mỗi người thứ ba mươi bảy.
Nếu bạn làm việc với dữ liệu quan trọng và thậm chí có khả năng từ xa là dữ liệu gốc sẽ cần được khôi phục, thì
5 không phải là lựa chọn tốt nhất. Nếu dữ liệu là một bản sao, nếu nó là dữ liệu làm việc không quan trọng, nếu không ai có thể mất nó vì nó có thể được truy xuất, thì
4, danh sách ban đầu của các vận động viên vẫn còn nguyên vẹn và chưa được ghi đè. Yêu cầu ngẫu hứng của việc tìm kiếm mỗi người thứ ba mươi bảy để vượt qua vạch đích có thể được thực hiện bằng cách tương tác với các giá trị ban đầu:
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
29 được tạo bằng cách sử dụng sải chân trong cú pháp Slice Danh sách trên
>>> # Python 3>>> help(sorted)Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
15, vẫn chứa thứ tự ban đầu trong đó các vận động viên vượt qua vạch đích.
Cả hai đều có các đặc điểm rất khác nhau khi nói đến sửa đổi đầu ra và tại chỗ, vì vậy hãy đảm bảo bạn nghĩ thông qua bất kỳ chức năng hoặc chương trình ứng dụng nào sẽ sử dụng
Đối với Avid Pythonistas đang tìm kiếm một thách thức với việc sắp xếp, hãy thử sử dụng các loại dữ liệu phức tạp hơn trong việc sắp xếp: Nestables. Ngoài ra, hãy thoải mái đi sâu vào các triển khai mã Python nguồn mở cho các bản dựng và đọc về thuật toán sắp xếp được sử dụng trong Python có tên là Timsort. Và nếu bạn đang tìm cách sắp xếp từ điển, thì hãy kiểm tra sắp xếp từ điển Python: giá trị, khóa, v.v.
Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Sắp xếp dữ liệu bằng Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:
Sorting Data With Python
Làm thế nào để bạn sắp xếp một danh sách được đánh số?
Bạn có thể sắp xếp một danh sách đạn một cấp hoặc được đánh số để văn bản xuất hiện trong thứ tự theo thứ tự chữ cái tăng dần (a đến z) hoặc giảm dần (z đến a). Chọn danh sách bạn muốn sắp xếp. Đi về nhà> Sắp xếp. Sắp xếp theo đoạn văn và văn bản.Go to Home > Sort.Set Sort by to Paragraphs and Text.
Làm thế nào để bạn sắp xếp một danh sách các số theo thứ tự giảm dần trong Python?
Syntax of Sort () Hàm đảo ngược: (Tùy chọn), Reverse = true sẽ sắp xếp danh sách giảm dần.Mặc định là đảo ngược = sai.Tùy chọn chính.reverse: (Optional), reverse=True will sort the list descending. Default is reverse=False. key Optional.
Làm thế nào để bạn sắp xếp một danh sách với nhiều giá trị trong Python?
Để sắp xếp một danh sách các bộ dữ liệu theo nhiều phần tử trong Python: Chuyển danh sách cho hàm Sắp xếp (). Sử dụng đối số chính để chọn các phần tử tại các chỉ số cụ thể trong mỗi tuple.Hàm Sắp xếp () sẽ sắp xếp danh sách các bộ dữ liệu theo các phần tử được chỉ định.Pass the list to the sorted() function.Use the key argument to select the elements at the specific indices in each tuple. The sorted() function will sort the list of tuples by the specified elements.
Làm thế nào để bạn đặt số theo thứ tự tăng dần trong Python?
Sắp xếp (), không có đối số hoặc tham số bổ sung, đang đặt hàng các giá trị theo số theo thứ tự tăng dần, có nghĩa là nhỏ nhất đến lớn nhất.Biến số ban đầu không thay đổi vì Sắp xếp () cung cấp đầu ra được sắp xếp và không thay đổi giá trị ban đầu tại chỗ. , with no additional arguments or parameters, is ordering the values in numbers in an ascending order, meaning smallest to largest. The original numbers variable is unchanged because sorted() provides sorted output and does not change the original value in place.