Nếu trước tiên chúng tôi sắp xếp theo lượt tải xuống, sau đó tất cả các mục không có lượt tải xuống nào được sắp xếp theo ngày, chúng tôi có thể làm như vậy bằng cách sử dụng đối số
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
0 cho def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
1. Đối với đối số def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
0, chúng tôi chỉ định một hàm trả về giá trị sẽ được sử dụng để sắp xếp các mục. Nếu giá trị được trả về là một bộ, thì nó sẽ sắp xếp theo giá trị đầu tiên và sau đó là giá trị thứ haisorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
Thay vì sử dụng lambda, chúng ta cũng có thể truyền một hàm riêng
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
TypeError khi tuần tự hóa thể hiện của lớp thành JSON
Lý do TypeErrors khi tuần tự hóa các thể hiện của lớp thành JSON là do bộ mã hóa JSON
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
3 chỉ biết cách tuần tự hóa một tập hợp giới hạn các loại đối tượng theo mặc định, tất cả các loại tích hợp sẵnMột giải pháp có thể là viết một lớp kế thừa từ
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
4 và sau đó triển khai hàm def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
5Một giải pháp đơn giản khác là gọi
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
3 trên thành viên sorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
1 của trường hợp đóclass Foo[object]:
def __init__[self]:
self.x = 1
self.y = 2
foo = Foo[]
s = json.dumps[foo] # raises TypeError with "is not JSON serializable"
s = json.dumps[foo.__dict__] # s set to: {"x":1, "y":2}
"Có thể đăng ký"
Có thể đăng ký có nghĩa là đối tượng thực hiện phương thức
sorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
2. Nói cách khác, nó mô tả các đối tượng là "vật chứa. " Các loại có thể đăng ký bao gồm chuỗi, danh sách, bộ dữ liệu và từ điển Không, bạn không thể sắp xếp
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
7 theo tổng số, vì một phần tử của def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
7 có thể có cả giá thấp nhất và giá cao nhất và cần phải ở cả đầu và cuối của quá trình sắp xếpChúng tôi có thể tiến gần đến những gì bạn muốn, nhưng trước tiên
Bạn đang xây dựng
def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
9 như một từ điển, nhưng không bao giờ sử dụng nó như một từ điển. Bạn chỉ đang sử dụng nó như một thùng chứa và nhận được sorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
0Tệ hơn nữa, bạn có khả năng bị mất giá trị vì bạn gán cho
sorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
1. Nếu "walmar" có sản phẩm "tps5", nó sẽ xung đột với "walmart" có "ps5"Thay vào đó, bạn chỉ có thể duy trì một danh sách các mục
items = []
for domain, products in ...:
for product in ...:
...
items.append[...]
sorted_items = sorted[items, ...]
Bạn đang xây dựng từ điển 3 thành phần với các khóa cố định theo thứ tự cố định
sorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
2Bạn chỉ có thể sử dụng một
sorted[your_list, key=lambda x: [your_dict[x]['downloads'], your_dict[x]['date']]]
3. from typing import NamedTuple
class Item[NamedTuple]:
source: str
product: str
total: int # Or maybe float?
items = []
for domain, products in ...:
for product in ...:
...
items.append[Item[domain, product, total]]
sorted_items = sorted[items, key=lambda item: item.total, reverse=True]
for item in sorted_items:
print[f"{item.source},{item.product},{item.total}"]
0, mặc dù một from typing import NamedTuple
class Item[NamedTuple]:
source: str
product: str
total: int # Or maybe float?
items = []
for domain, products in ...:
for product in ...:
...
items.append[Item[domain, product, total]]
sorted_items = sorted[items, key=lambda item: item.total, reverse=True]
for item in sorted_items:
print[f"{item.source},{item.product},{item.total}"]
1 sẽ tốt hơnfrom typing import NamedTuple
class Item[NamedTuple]:
source: str
product: str
total: int # Or maybe float?
items = []
for domain, products in ...:
for product in ...:
...
items.append[Item[domain, product, total]]
sorted_items = sorted[items, key=lambda item: item.total, reverse=True]
for item in sorted_items:
print[f"{item.source},{item.product},{item.total}"]
Cuối cùng,
from typing import NamedTuple
class Item[NamedTuple]:
source: str
product: str
total: int # Or maybe float?
items = []
for domain, products in ...:
for product in ...:
...
items.append[Item[domain, product, total]]
sorted_items = sorted[items, key=lambda item: item.total, reverse=True]
for item in sorted_items:
print[f"{item.source},{item.product},{item.total}"]
2 [hoặc def keyfunc[tup]:
key, d = tup
return d["downloads"], d["date"]
items = sorted[d.items[], key = keyfunc]
9 trong trường hợp của bạn] là một thùng chứa tạm thời chỉ cần thiết để lưu trữ các mục trong danh sách được sắp xếp. Bạn không cần lưu trữ tạm thời đó trong một biến đã đặt tên;