Python sắp xếp danh sách các bộ dữ liệu theo phần tử đầu tiên rồi đến phần tử thứ hai

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 name
2 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 name
3 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 name
4 để 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 name
5 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 name
0] 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 name
1 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 name
2 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 name
4 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 name
3. Để đả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 name
1 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 name
2 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 name
3

Ý 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 name
2 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 name
3. 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

Làm cách nào để sắp xếp danh sách các bộ dữ liệu trong Python theo phần tử thứ nhất và thứ hai?

Việc sắp xếp danh sách trong Python có thể dễ dàng thực hiện bằng cách sử dụng sorted[] . Hàm sorted[] trả về một danh sách với các phần tử được sắp xếp theo thứ tự tăng dần hoặc giảm dần. Cú pháp của hàm này là. đã sắp xếp [ danh sách , khóa =. , đảo ngược =. ]

Bạn có thể sắp xếp danh sách các bộ dữ liệu trong Python không?

Phương pháp số 1. Sử dụng kỹ thuật Sắp xếp bong bóng Sử dụng kỹ thuật Sắp xếp bong bóng để chúng ta có thể thực hiện việc sắp xếp. Lưu ý rằng mỗi bộ là một phần tử trong danh sách đã cho. Truy cập phần tử thứ hai của mỗi bộ bằng các vòng lặp lồng nhau. Điều này thực hiện phương pháp sắp xếp tại chỗ

Bạn có thể đặt hàng với các bộ dữ liệu không?

Tuple mục được sắp xếp theo thứ tự, không thể thay đổi và cho phép các giá trị trùng lặp . Các mục Tuple được lập chỉ mục, mục đầu tiên có chỉ mục [0], mục thứ hai có chỉ mục [1], v.v.

Chủ Đề