美文网首页
python sorted

python sorted

作者: 盘古_创世纪 | 来源:发表于2020-05-26 14:33 被阅读0次

    a_dic= {'a': {'1': 3}, 'b': {'5': 4}, 'c': {'3': 1}, 'd': {'2': 99}}
    b_= sorted(a_dic.values(), key=lambda item: list(item.keys())[0])
    print(b_)

    example_list= [5, 0, 6, 1, 2, 7, 3, 4]
    result_list= sorted(example_list, key=lambda x: -x)
    print(result_list)

    array= [{"age": 20, "name": "a"}, {"age": 25, "name": "b"}, {"age": 10, "name": "c"}]
    array_= sorted(array, key=lambda x: x["age"])
    print(array_)

    L= [('d', 2), ('a', 4), ('b', 3), ('c', 2)]
    l= sorted(L, key=lambda x: x[1])
    print(l)

    # 先按照成绩降序排序,相同成绩的按照名字升序排序
    d1= [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}]
    d1_= sorted(d1, key=lambda x: (-x['score'], x['name']))
    print(d1_)

    a_dic= {'a': {'1': 3}, 'b': {'5': 4}, 'c': {'3': 1}, 'd': {'2': 99}}
    print(a_dic.values())
    a_= sorted(a_dic.values(), key=lambda x: list(x.keys()))
    print(a_)

    arr_= [{'1': 3}, {'5': 4}, {'3': 1}, {'2': 99}]
    brr_= sorted(arr_, key=lambda item: list(item.keys())[0])
    print(brr_)

    相关文章

      网友评论

          本文标题:python sorted

          本文链接:https://www.haomeiwen.com/subject/zlamahtx.html