Timsort là một thuật toán sắp xếp lai, có nguồn gốc từ sắp xếp hợp nhất và sắp xếp chèn, được thiết kế để thực hiện tốt trên nhiều loại dữ liệu trong thế giới thực. Nó được phát minh bởi Tim Peters vào năm 2002 để sử dụng trong ngôn ngữ lập trình Python. Thuật toán tìm thấy các tập hợp con của dữ liệu đã được đặt hàng và sử dụng các tập hợp con để sắp xếp dữ liệu hiệu quả hơn. Điều này được thực hiện bằng cách hợp nhất một tập hợp con được xác định, được gọi là chạy, với các lần chạy hiện có cho đến khi các tiêu chí nhất định được đáp ứng. Timsort đã là thuật toán sắp xếp tiêu chuẩn của Python kể từ phiên bản 2.3. Bây giờ nó cũng được sử dụng để sắp xếp các mảng trong Java SE 7 và trên nền tảng Android.
Đã trả lời ngày 8 tháng 6 năm 2012 lúc 12:35Jun 8, 2012 at 12:35
4
Thuật toán sắp xếp được gọi là Timsort. Xem Timsort
Đã trả lời ngày 8 tháng 6 năm 2012 lúc 12:35Jun 8, 2012 at 12:35
Thuật toán sắp xếp được gọi là Timsort. Xem TimsortPierce
Piercepiierce2
silver badges8 bronze badges
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.
4 sẽ không đối xử với một câu khác nhau, và nó sẽ sắp xếp từng ký tự, bao gồm cả khoảng trắng.
>>> 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'
6 có thể thay đổi hành vi này và làm sạch đầu ra, và
>>> 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'
7 có thể đặt tất cả lại với nhau. Chúng tôi sẽ bao gồm thứ tự cụ thể của đầu ra và tại sao nó lại như vậy:
>>>
>>> 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'
Câu gốc trong ví dụ này được chuyển đổi thành một danh sách các từ thay vì để lại nó dưới dạng
4 trên danh sách chứa dữ liệu không thể so sánh. Trong ví dụ này, mộ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'
3 và
>>> 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 trong cùng một danh sách có thể được sắp xếp vì sự không tương thích của chú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'
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
Hãy cùng trở lại ví dụ trước đó về việc sắp xếp theo chữ cái đầu tiên khi vụ án khác nhau.
2 có thể được sử dụng để giải quyết vấn đề đó bằng cách chuyển đổi toàn bộ chuỗi thành chữ thườ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.
1
Các giá trị đầu ra chưa được chuyển đổi thành chữ thường 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.
2
Ví dụ dưới đây cho thấy định nghĩa của một hàm bổ sung có hai đối số. Khi hàm đó được sử dụng trong
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
>>> # 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.
3
Phải được định nghĩa nội tuyến
Nếu yêu cầu thay đổi và thứ tự cũng phải được đảo ngược, thì từ khóa
>>> # 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.
72 dựa trên một thuộc tính. Nếu bạn có một nhóm học sinh và cần phải sắp xếp chúng theo lớp cuối cùng của họ, cao nhất đến thấp nhất, thì có thể 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.
72 dựa trên một thuộc tính. Nếu bạn có một nhóm học sinh và cần phải sắp xếp chúng theo lớp cuối cùng của họ, cao nhất đến thấp nhất, thì có thể sử dụng
>>> None<0Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'NoneType' and 'int'
9 để làm cho đầu ra tăng dần bị lật để giảm xuống để các lớp cao nhất được đặt hàng đầu tiên.
Các khả năng là vô tận cho cách đặt hàng có thể được thực hiện khi bạn tận dụng cả đối số từ khóa >>> 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 và >>> 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']
3 trê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. Mã có thể được giữ sạch sẽ và ngắn khi bạn sử dụng >>> numbers = [6, 9, 3, 1]
>>> numbers_sorted = sorted(numbers)
>>> numbers_sorted
[1, 3, 6, 9]
>>> numbers
[6, 9, 3, 1]
60 cơ bản cho một hàm nhỏ hoặc bạn có thể viết một chức năng hoàn toàn mới, nhập nó và sử dụng nó trong đối số chính.
>>> # 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.
72 dựa trên một thuộc tính. Nếu bạn có một nhóm học sinh và cần phải sắp xếp chúng theo lớp cuối cùng của họ, cao nhất đến thấp nhất, thì có thể 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.
95 và chỉ có thể được sử dụng với các danh sách. Nó không phải là một tích hợp với một điều khác được truyền cho 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.
>>> 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ã:
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.
Những khác biệt trong hành vi này tạo ra >>> 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]
5 và >>> 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 hoàn toàn không thể hoán đổi cho mã và chúng có thể tạo ra kết quả cực kỳ bất ngờ nếu một người được sử dụng sai cá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.
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:
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:
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:
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. 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
Loại nào là loại () trong Python?
Sắp xếp mặc định của Python sử dụng Tim sắp xếp, đây là sự kết hợp của cả sắp xếp hợp nhất và sắp xếp chèn.Tim Sort, which is a combination of both merge sort and insertion sort.
Loại nào sắp xếp () sử dụng?
Thuật toán được sử dụng bởi sort () là phần giới thiệu.Introsort là một thuật toán sắp xếp lai sử dụng thuật toán sắp xếp ba để giảm thiểu thời gian chạy, nhanh chóng, phân loại Heapsort và chèn.Quicksort, Heapsort and Insertion Sort.
Phương thức loại () là gì?
Phương thức Sắp xếp () sắp xếp các phần tử của một mảng tại chỗ và trả về tham chiếu đến cùng một mảng, hiện được sắp xếp.Thứ tự sắp xếp mặc định là tăng dần, được xây dựng khi chuyển đổi các yếu tố thành các chuỗi, sau đó so sánh các chuỗi các giá trị đơn vị mã UTF-16 của chúng.sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
Làm thế nào để python sắp xếp () hoạt động?
Hàm python sort () hàm Sắp xếp () Trả về một danh sách được sắp xếp của đối tượng có thể lặp lại được chỉ định.Bạn có thể chỉ định thứ tự tăng dần hoặc giảm dần.Chuỗi được sắp xếp theo thứ tự bảng chữ cái, và các số được sắp xếp bằng số.Lưu ý: Bạn không thể sắp xếp một danh sách chứa cả giá trị chuỗi và giá trị số.returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.