python中map的排序以及取出map中取最大最小值

map排序:
1.按key排序:
items=dict.items()
items.sort()
 
sorted(dict.items(),key=lambda x:x[0],reverse=False)
 
2.按value排序
sorted(dict.items(),key=lambda x:x[1],reverse=False)
 
(ps:在python2.x中还是有cmp函数的,在3.x中已经没有了,但是引入了
import operator       #首先要导入运算符模块
operator.gt(1,2)      #意思是greater than(大于)
operator.ge(1,2)      #意思是greater and equal(大于等于)
operator.eq(1,2)      #意思是equal(等于)
operator.le(1,2)      #意思是less and equal(小于等于)
operator.lt(1,2)      #意思是less than(小于)
)
map取最大最小值:
方法一:
max(dict,key=dict.get)
min(dict,key=dict.get)
方法二:
min(d.items(), key=lambda x: x[1])
min(d.items(), key=lambda x: x[1][0]
min(d.items(), key=lambda x: x[1])[1]
实践:取零售价、促销价、会员价中价格最低的标志位。
def get_min_price(raw_retail_price,raw_promo_price,raw_vip_price):
    price_type = {}
    if raw_retail_price > 0:
        price_type["0"] = raw_retail_price
    if raw_promo_price > 0:
        price_type["1"] = raw_promo_price
    if raw_vip_price > 0:
        price_type["2"] = raw_vip_price
    discount_type = "0"
    if price_type:
        discount_type = min(price_type, key=price_type.get)
    return str(discount_type)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • CREATE TABLE IF NOT EXISTS ecs_order_info (order_id mediu...
    cookie口阅读 16,210评论 0 16
  • 时间:2019.8月27日 今日名言:你若安好,便是晴天 计划: (已完成)1——5:50起床 半小时锻炼 (已完...
    会挥舞的画笔阅读 265评论 0 0
  • 2008年5月19日 公元2008年5月19日14时28分,举国志哀,为七天前的这个时候发生在神州西南的那...
    明月劫阅读 216评论 0 0
  • 旅途的路上,偶尔停下匆忙的脚步,也许会发现你身边会有不错的风景。 有时候当一个人赶火车,赶飞机时,心中总是...
    兔子警长阅读 130评论 2 1

友情链接更多精彩内容