Sắp xếp bằng hai phím python

Trong Python, sắp xếp danh sách từ điển bằng phương thức

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 hoặc hàm
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0 sẽ gây ra lỗi
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
1 theo mặc định

Show

Bằng cách chỉ định tham số

pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
2 của
# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 hoặc
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0, bạn có thể sắp xếp danh sách từ điển theo giá trị của khóa cụ thể

Bài viết này mô tả các nội dung sau

  • Sắp xếp danh sách từ điển sẽ gây ra lỗi theo mặc định
  • Chỉ định biểu thức lambda cho tham số
    pprint.pprint(sorted(l, key=lambda x: x['Age']))
    # [{'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
    #  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
    
    pprint.pprint(sorted(l, key=lambda x: x['Name']))
    # [{'Age': 40, 'Name': 'Alice', 'Point': 80},
    #  {'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
    
    2
  • Chỉ định
    pprint.pprint(sorted(l, key=lambda x: x['Age']))
    # [{'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
    #  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
    
    pprint.pprint(sorted(l, key=lambda x: x['Name']))
    # [{'Age': 40, 'Name': 'Alice', 'Point': 80},
    #  {'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
    
    6 cho thông số
    pprint.pprint(sorted(l, key=lambda x: x['Age']))
    # [{'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
    #  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
    
    pprint.pprint(sorted(l, key=lambda x: x['Name']))
    # [{'Age': 40, 'Name': 'Alice', 'Point': 80},
    #  {'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
    
    2
  • Sắp xếp theo nhiều phím
  • pprint.pprint(sorted(l, key=lambda x: x['Age']))
    # [{'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
    #  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
    
    pprint.pprint(sorted(l, key=lambda x: x['Name']))
    # [{'Age': 40, 'Name': 'Alice', 'Point': 80},
    #  {'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
    
    8,
    pprint.pprint(sorted(l, key=lambda x: x['Age']))
    # [{'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
    #  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
    
    pprint.pprint(sorted(l, key=lambda x: x['Name']))
    # [{'Age': 40, 'Name': 'Alice', 'Point': 80},
    #  {'Age': 20, 'Name': 'Bob'},
    #  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
    
    9 để biết danh sách từ điển

Các mã mẫu sau đây sử dụng danh sách từ điển có khóa chung. Mô-đun pprint được sử dụng để làm cho đầu ra dễ đọc hơn

  • In đẹp với pprint trong Python

import pprint

l = [{'Name': 'Alice', 'Age': 40, 'Point': 80},
     {'Name': 'Bob', 'Age': 20},
     {'Name': 'Charlie', 'Age': 30, 'Point': 70}]

nguồn.

Liên kết được tài trợ

Sắp xếp danh sách từ điển sẽ gây ra lỗi theo mặc định

Sắp xếp danh sách từ điển (_______12_______0) bằng phương pháp

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 hoặc hàm
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0 sẽ gây ra lỗi
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
1 theo mặc định

Điều này là do từ điển không hỗ trợ so sánh với

pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]
4,
pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]
5, v.v.

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'

nguồn.

Chỉ định biểu thức lambda cho tham số pprint.pprint(sorted(l, key=lambda x: x['Age'])) # [{'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}, # {'Age': 40, 'Name': 'Alice', 'Point': 80}] pprint.pprint(sorted(l, key=lambda x: x['Name'])) # [{'Age': 40, 'Name': 'Alice', 'Point': 80}, # {'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}] 2

Để sắp xếp danh sách từ điển theo giá trị của khóa cụ thể, hãy chỉ định tham số

pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
2 của phương thức
# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 hoặc hàm
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0

Bằng cách chỉ định một chức năng được áp dụng cho từng thành phần của danh sách, nó được sắp xếp theo kết quả của chức năng đó. Xem bài viết sau để biết thêm thông tin

  • Cách sử dụng tham số chính trong Python (được sắp xếp, tối đa, v.v. )

Trong ví dụ này, bạn có thể chỉ định một hàm để lấy giá trị của một khóa cụ thể từ từ điển

Bạn có thể định nghĩa một hàm với

# sorted(l, key=lambda x: x['Point'])
# KeyError: 'Point'
0, nhưng sẽ thuận tiện hơn khi sử dụng các biểu thức lambda trong trường hợp như vậy

  • Biểu thức Lambda trong Python

pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]

nguồn.

Chỉ định sắp xếp theo thứ tự giảm dần hay tăng dần với tham số

# sorted(l, key=lambda x: x['Point'])
# KeyError: 'Point'
1

pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]

nguồn.

Các ví dụ cho đến nay sử dụng

pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0, nhưng bạn có thể chỉ định
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
2 và
# sorted(l, key=lambda x: x['Point'])
# KeyError: 'Point'
1 theo cách tương tự với phương pháp
# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 của
# sorted(l, key=lambda x: x['Point'])
# KeyError: 'Point'
6

Để biết sự khác biệt giữa

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 và
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0, hãy xem bài viết sau.
# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9 tự sắp xếp đối tượng ban đầu và
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0 tạo một đối tượng được sắp xếp mới

  • Sắp xếp danh sách, chuỗi, bộ trong Python (sắp xếp, sắp xếp)

Khi khóa được chỉ định không tồn tại

Với cách hiển thị ở trên, sẽ xảy ra lỗi nếu khóa được chỉ định không tồn tại

# sorted(l, key=lambda x: x['Point'])
# KeyError: 'Point'

nguồn.

Trong trường hợp như vậy, hãy sử dụng phương thức

# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
1 của
pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]
0, phương thức này trả về giá trị mặc định cho các khóa không tồn tại

  • Nhận giá trị từ từ điển theo khóa với get() trong Python

Theo mặc định,

# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
1 trả về
# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
4 cho các khóa không tồn tại.
# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
4 không thể so sánh với một số hoặc một chuỗi, vì vậy sẽ xảy ra lỗi

# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'

nguồn.

Bạn có thể chỉ định một giá trị cho khóa không tồn tại làm đối số thứ hai của

# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
1. Các phần tử có khóa không tồn tại được thay thế bằng giá trị được chỉ định trong đối số thứ hai và được sắp xếp

pprint.pprint(sorted(l, key=lambda x: x.get('Point', 75)))
# [{'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

nguồn.

Vô cực

# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
7 được xác định là lớn hơn bất kỳ số nào khác, vì vậy bạn có thể sử dụng
# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
7 và
# sorted(l, key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
9 để luôn đặt các phần tử không có khóa ở cuối hoặc đầu

  • "inf" cho vô cực trong Python

pprint.pprint(sorted(l, key=lambda x: x.get('Point', float('inf'))))
# [{'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'}]

pprint.pprint(sorted(l, key=lambda x: x.get('Point', -float('inf'))))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

nguồn.

Chỉ định pprint.pprint(sorted(l, key=lambda x: x['Age'])) # [{'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}, # {'Age': 40, 'Name': 'Alice', 'Point': 80}] pprint.pprint(sorted(l, key=lambda x: x['Name'])) # [{'Age': 40, 'Name': 'Alice', 'Point': 80}, # {'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}] 6 cho thông số pprint.pprint(sorted(l, key=lambda x: x['Age'])) # [{'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}, # {'Age': 40, 'Name': 'Alice', 'Point': 80}] pprint.pprint(sorted(l, key=lambda x: x['Name'])) # [{'Age': 40, 'Name': 'Alice', 'Point': 80}, # {'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}] 2

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

pprint.pprint(sorted(l, key=lambda x: x.get('Point', 75)))
# [{'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
2 của mô-đun toán tử của thư viện tiêu chuẩn. Nó nhanh hơn sử dụng biểu thức lambda

  • Mô-đun toán tử trong Python (itemgetter, attrgetter, methodcaller)

import operator

pprint.pprint(sorted(l, key=operator.itemgetter('Age')))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=operator.itemgetter('Name')))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]

nguồn.

Nếu khóa được chỉ định không tồn tại, sẽ xảy ra lỗi

# sorted(l, key=operator.itemgetter('Point'))
# KeyError: 'Point'

nguồn.

Liên kết được tài trợ

Sắp xếp theo nhiều phím

Sau đây là một ví dụ về trường hợp các từ điển có cùng giá trị cho một khóa chung. Hai từ điển có giá trị

pprint.pprint(sorted(l, key=lambda x: x.get('Point', 75)))
# [{'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
3 cho khóa
pprint.pprint(sorted(l, key=lambda x: x.get('Point', 75)))
# [{'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]
4

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
0

nguồn.

Nếu các giá trị bằng nhau, thứ tự ban đầu được giữ nguyên

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
1

nguồn.

Bạn có thể chỉ định nhiều đối số cho

pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
6 và nếu các giá trị cho khóa đầu tiên bằng nhau, chúng sẽ được so sánh và sắp xếp theo giá trị của khóa tiếp theo

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
2

nguồn.

Lưu ý rằng nếu thứ tự của các đối số khác nhau, kết quả cũng khác nhau

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
3

nguồn.

Điều tương tự cũng có thể được thực hiện với các biểu thức lambda trả về nhiều giá trị dưới dạng bộ dữ liệu hoặc danh sách

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
4

nguồn.

pprint.pprint(sorted(l, key=lambda x: x['Age'])) # [{'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}, # {'Age': 40, 'Name': 'Alice', 'Point': 80}] pprint.pprint(sorted(l, key=lambda x: x['Name'])) # [{'Age': 40, 'Name': 'Alice', 'Point': 80}, # {'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}] 8, pprint.pprint(sorted(l, key=lambda x: x['Age'])) # [{'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}, # {'Age': 40, 'Name': 'Alice', 'Point': 80}] pprint.pprint(sorted(l, key=lambda x: x['Name'])) # [{'Age': 40, 'Name': 'Alice', 'Point': 80}, # {'Age': 20, 'Name': 'Bob'}, # {'Age': 30, 'Name': 'Charlie', 'Point': 70}] 9 để biết danh sách từ điển

Như đã đề cập ở trên, so sánh với

pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]
4 hoặc
pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]
5 không được hỗ trợ cho từ điển
pprint.pprint(sorted(l, key=lambda x: x['Age'], reverse=True))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 20, 'Name': 'Bob'}]
0, vì vậy việc chuyển danh sách từ điển tới
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
8 hoặc
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
9 sẽ gây ra lỗi

# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
5

nguồn.

Như với

pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
0 và
# sorted(l)
# TypeError: '<' not supported between instances of 'dict' and 'dict'
9, bạn cũng có thể chỉ định tham số
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
2 trong
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
8 và
pprint.pprint(sorted(l, key=lambda x: x['Age']))
# [{'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70},
#  {'Age': 40, 'Name': 'Alice', 'Point': 80}]

pprint.pprint(sorted(l, key=lambda x: x['Name']))
# [{'Age': 40, 'Name': 'Alice', 'Point': 80},
#  {'Age': 20, 'Name': 'Bob'},
#  {'Age': 30, 'Name': 'Charlie', 'Point': 70}]
9

Khóa hoạt động như thế nào trong Python được sắp xếp?

Để sắp xếp tùy chỉnh phức tạp hơn, sorted() nhận một "key=" tùy chọn chỉ định hàm "key" biến đổi từng phần tử trước khi so sánh. The key function takes in 1 value and returns 1 value, and the returned "proxy" value is used for the comparisons within the sort.

Sự khác biệt giữa hàm sort() và sorted() giải thích bằng ví dụ là gì?

sort() về cơ bản hoạt động với chính danh sách đó. Nó sửa đổi danh sách ban đầu tại chỗ. Giá trị trả về là Không có. sorted() hoạt động trên bất kỳ lần lặp nào có thể bao gồm danh sách, từ điển, v.v. .

Làm cách nào để sử dụng bộ so sánh trong Python?

Bạn cũng có thể sử dụng sorted() với một bộ so sánh tùy chỉnh làm tham số của nó. Trong Python 2, sorted() có thể được triển khai bằng một bộ so sánh tùy chỉnh, cmp hoặc tham số chính. .
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