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]

 

题目大意:

计蒜客第6题泥塑课

基本思路:

不需要思路

代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import operator
import math
while(True):
    n=int(input())
    if n==-1:
        break
    dict={}
    for i in range(0,n):
        l,w,h,name=input().strip().split()
        v=int(l)*int(w)*int(h)
        dict[name]=v
    print("{} took clay from {}.".format(max(dict,key=dict.get),min(dict,key=dict.get)))

  

原文地址:https://www.cnblogs.com/imzscilovecode/p/8762876.html