我希望以相反的顺序计算我的计数
这是我的代码
def get_products_most_sold(**kwargs):
code = kwargs['code']
sales_from = kwargs['from_date']
sales_to = kwargs['to_date']
times_sold = products_by_date_store(code, sales_from, sales_to)
products={}
barcodes=times_sold.keys()
for barcode in barcodes:
log.info(barcode)
product=Product.get_by_bacode_storecode(barcode,code)
if product is not None:
products[barcode]=product.json()
return_pr=lambda p: None if products.get(p) is None else products.get(p)
return_sn=lambda p: None if products.get(p) is None else Supplier.get_supplier_name(products.get(p)['supplier_code'],products.get(p)['store_code'])
pr = lambda p: {'product':return_pr(p),'count':times_sold.get(p),'supplier_name':return_sn(p)}
return [pr(item) for item in barcodes]
答案 0 :(得分:1)
您可以使用sorted
功能。并将key
参数指定为返回计数的函数。使用reverse=True
参数进行降序排序。
return sorted([pr(item) for item in barcodes], key=lambda item: item['count'], reverse=True)