【每日一题0616】 按字典的value值排序

d={'A':10,'B':11,'C':"8"}
按value值排序后     {'C':"8",'A':10,'B':11}

题解:

d={'A':10,'B':11,'C':"8"}

def order(inp_dict):
    return dict(sorted(inp_dict.items(),key=lambda x:int(x[1])))

assert {'C':"8",'A':10,'B':11} == order(d)

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

1 个赞
def order_by_value(d:dict):
    """ 根据value值升序排列"""
    # d = {'D': 12, 'B': 11, 'C': "8"}
    return dict(sorted(d.items(), key=lambda x:int(x[1])))
1 个赞
def sortFunc(dictData, result={}):
    L=list(dictData.values())
    for i in range(len(L)):
        L[i] = int(L[i])
    L.sort()

    for item in L:
        for key in list(dictData.keys()):
            if int(dictData[key]) == item:
                result[key] = dictData[key]
    return result

assert sortFunc({'A':10,'B':11,'C':"8"}) == {'C':"8",'A':10,'B':11}