按品牌按利润百分比排序集合
创始人
2024-11-04 14:30:12
0

假设有以下数据:

collections = [
    {'brand': 'Nike', 'profit': 1000, 'revenue': 2000},
    {'brand': 'Adidas', 'profit': 2000, 'revenue': 4000},
    {'brand': 'Puma', 'profit': 500, 'revenue': 1000},
    {'brand': 'Under Armour', 'profit': 1500, 'revenue': 3000}
]

可以使用sorted()函数进行排序,并传递一个排序函数作为其key参数,该函数将返回每个品牌的利润百分比。

def sort_by_profit_percentage(item):
    profit_percentage = (item['profit'] / item['revenue']) * 100
    return profit_percentage

sorted_collections = sorted(collections, key=sort_by_profit_percentage, reverse=True)

for collection in sorted_collections:
    print(collection)

输出结果为:

{'brand': 'Adidas', 'profit': 2000, 'revenue': 4000}
{'brand': 'Under Armour', 'profit': 1500, 'revenue': 3000}
{'brand': 'Nike', 'profit': 1000, 'revenue': 2000}
{'brand': 'Puma', 'profit': 500, 'revenue': 1000}

这意味着首先按品牌利润百分比从高到低排序,然后按降序排列结果集。

相关内容

热门资讯

推荐透视!哈糖大菠萝能开挂吗,... 推荐透视!哈糖大菠萝能开挂吗,pokerworld辅助器,方针教程(有挂教学)-哔哩哔哩1、每一步都...
解密透视!sohoo poke... 解密透视!sohoo poker辅助器!一直是真的有辅助神器(有挂教学)-哔哩哔哩1、下载好soho...
解谜透视!拱趴大菠萝万能挂!切... 解谜透视!拱趴大菠萝万能挂!切实有辅助工具(有挂规律)-哔哩哔哩在进入拱趴大菠萝万能挂软件靠谱后,参...
教你透视!aapoker怎么选... 教你透视!aapoker怎么选牌,德州局透视脚本免费版下载手机版,方式教程(存在有挂)-哔哩哔哩在进...
教你透视!大菠萝免费辅助器!好... 教你透视!大菠萝免费辅助器!好像是有辅助教程(真的有挂)-哔哩哔哩1、大菠萝免费辅助器辅助器安装包、...
开挂透视!steampoker... 开挂透视!steampokermaster辅助,红龙poker辅助器免费观看,妙计教程(果真有挂)-...
曝光透视!红龙poker有辅助... 曝光透视!红龙poker有辅助吗!竟然真的是有辅助软件(有挂分析)-哔哩哔哩1、完成红龙poker有...
辅助透视!hhpoker脚本下... 辅助透视!hhpoker脚本下载,wepoker辅助器激活码,妙招教程(有挂教学)-哔哩哔哩1、下载...
曝光透视!sohoo poke... 曝光透视!sohoo poker辅助!竟然一直都是有辅助脚本(有挂存在)-哔哩哔哩1、全新机制【so...
总结透视!fishpoker透... 总结透视!fishpoker透视底牌,wepoker有辅助器吗,总结教程(有挂教程)-哔哩哔哩1、游...