Python---进阶---函数式编程---按照权重排序
一、
权重是100
价格占的权重是40%,销量占的权重是17%,评级站的权重是13%,评论占的权重是30%
---------------------------------------------------------------------------------------------------------
goods = [{"name":"good1", "price":200, "sales":100, "stars":5, "comments":400},
{"name":"good2", "price":300, "sales":120, "stars":4, "comments":500},
{"name":"good3", "price":500, "sales":3000, "stars":2, "comments":199},
{"name":"good4", "price":1288, "sales":1288, "stars":5, "comments":398},
{"name":"good5", "price":899, "sales":899, "stars":5, "comments":2000}]
{"name":"good2", "price":300, "sales":120, "stars":4, "comments":500},
{"name":"good3", "price":500, "sales":3000, "stars":2, "comments":199},
{"name":"good4", "price":1288, "sales":1288, "stars":5, "comments":398},
{"name":"good5", "price":899, "sales":899, "stars":5, "comments":2000}]
print(goods)
#sorted()
def my_sorted(arg):
price = arg["price"]
sales = arg['sales']
star = arg['stars']
comment = arg['comments']
data = price*0.4+sales*0.17+star*0.13+comment*0.3
return data
print(sorted(goods, key=my_sorted))
def my_sorted(arg):
price = arg["price"]
sales = arg['sales']
star = arg['stars']
comment = arg['comments']
data = price*0.4+sales*0.17+star*0.13+comment*0.3
return data
print(sorted(goods, key=my_sorted))
r = sorted(goods, key = lambda x: x['price'] * 0.4 + x['sales']*0.13 + x['comments']*0.13, reverse=True)
print(r)
------------------------------------------------
二、
------------------------------------------------