Việc chuyển một hàm so sánh cho phương thức
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name2 của danh sách là chậm đối với các danh sách có kích thước lớn, nhưng nó vẫn có thể khá tiện lợi khi bạn cần sắp xếp các danh sách có kích thước hợp lý. Đặc biệt, nó cung cấp một thành ngữ khá tự nhiên để sắp xếp theo nhiều trường
import string star_list = ['Elizabeth Taylor', 'Bette Davis', 'Hugh Grant', 'C. Grant'] star_list.sort[lambda x,y: [ cmp[string.split[x][-1], string.split[y][-1]] or # Sort by last name... cmp[x, y]]] # ...then by first name print "Sorted list of stars:" for name in star_list: print name
Công thức này sử dụng các thuộc tính của hàm dựng sẵn
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name3 và toán tử
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name4 để tạo thành ngữ nhỏ gọn để sắp xếp danh sách trên nhiều trường của mỗi mục
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name5 trả về false [
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name0] khi
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name1 và
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name2 so sánh bằng nhau, vì vậy chỉ trong những trường hợp này,
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name4 mới cho phép gọi tiếp theo tới
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name3. Để đảo ngược thứ tự sắp xếp, chỉ cần hoán đổi
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name1 và
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name2 làm đối số thành
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name3
Ý tưởng cơ bản của công thức này cũng có thể được sử dụng nếu một tiêu chí sắp xếp khác được liên kết với các phần tử của danh sách. Chúng ta chỉ cần xây dựng một danh sách các bộ phụ trợ để đóng gói tiêu chí sắp xếp cùng với các phần tử chính, sau đó sắp xếp và giải nén kết quả. Điều này giống với thành ngữ DSU hơn
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name
Tất nhiên, một khi chúng ta đang thực hiện việc trang trí, sắp xếp và không trang trí, có thể cần thêm một chút rắc rối để có thể gọi bước sắp xếp mà không có chức năng so sánh [thành ngữ DSU], điều này sẽ tăng tốc toàn bộ khá nhiều . Xét cho cùng, việc đóng gói các trường được so sánh theo đúng thứ tự trong mỗi bộ được trang trí và lấy lại trường phù hợp trong bước không trang trí là khá đơn giản
# Pack a better-ordered auxiliary list: aux_list = map[lambda x: [sorting_criterion_1[x], sorting_criterion_2[x], x], star_list] # Sort in a much simpler and faster way: aux_list.sort[ ] # Unpack the resulting list: star_list = map[lambda x: x[-1], aux_list]
Tuy nhiên, điều này không giải quyết được thứ tự đảo ngược mà bạn có thể dễ dàng nhận được khi chuyển một hàm so sánh sang
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name2 chỉ bằng cách chuyển đối số sang
def sorting_criterion_1[data]: return string.split[data][-1] # This is again the last name def sorting_criterion_2[data]: return len[data] # This is some fancy sorting criterion # Pack an auxiliary list: aux_list = map[lambda x: [x, sorting_criterion_1[x], sorting_criterion_2[x]], star_list] # Sort: aux_list.sort[lambda x,y: [ cmp[x[1], y[1]] or # Sort by criteria 1 [last name]... cmp[y[2], x[2]] or # ...then by criteria 2 [in reverse order]... cmp[x, y]]] # ...then by the value in the main list # Unpack the resulting list: star_list = map[lambda x: x[0], aux_list] print "Another sorted list of stars:" for name in star_list: print name3. Thay vào đó, để sử dụng DSU, bạn cần đóng gói giá trị đã thay đổi phù hợp của trường tiêu chí. Với trường số thì đổi dấu cũng được. Trong ví dụ này,
# Pack a better-ordered auxiliary list: aux_list = map[lambda x: [sorting_criterion_1[x], sorting_criterion_2[x], x], star_list] # Sort in a much simpler and faster way: aux_list.sort[ ] # Unpack the resulting list: star_list = map[lambda x: x[-1], aux_list]0 cần sắp xếp ngược thực sự là một số, vì vậy nhiệm vụ của chúng ta rất dễ dàng
# Pack a better-ordered auxiliary list yielding the desired order: aux_list = map[lambda x: [sorting_criterion_1[x], -sorting_criterion_2[x], x], star_list]
Để sắp xếp ngược trên trường chuỗi bằng DSU, bạn cần thao tác dịch chuỗi để ánh xạ từng
# Pack a better-ordered auxiliary list: aux_list = map[lambda x: [sorting_criterion_1[x], sorting_criterion_2[x], x], star_list] # Sort in a much simpler and faster way: aux_list.sort[ ] # Unpack the resulting list: star_list = map[lambda x: x[-1], aux_list]1 thành
# Pack a better-ordered auxiliary list: aux_list = map[lambda x: [sorting_criterion_1[x], sorting_criterion_2[x], x], star_list] # Sort in a much simpler and faster way: aux_list.sort[ ] # Unpack the resulting list: star_list = map[lambda x: x[-1], aux_list]2—hoặc một bảng dịch rộng hơn nữa cho chuỗi Unicode. Hơi phiền nhưng viết 1 lần thôi. Ví dụ: đối với các chuỗi cũ đơn giản