sorted倒序

'''
sorted 可以排列list, reverse=True 可以倒序排列

'''


#
# def list_Dict(x):
#     m = {}
#     for k,v in enumerate(x):
#         m[k]=v
#     print(m)
#     return m
#
# def sorted_dict(x):
#     print(x)
#     a = sorted(x.values(),reverse=True)
#     print('sorted_dict:',a)
#     return a


def sorted_dict(x):
    def ss(*args):
        print(*args)
        b = x(*args)
        a = sorted(b.values(),reverse=True)
        print('sorted_dict:',a)
        return b
    return ss



@sorted_dict
def list_Dict(x):
    m = {}
    for k,v in enumerate(x):
        m[k]=v
    print(m)
    return m



list_a = [1,2,3,5,12,9,23,99,49,23]


list_Dict(list_a)

 --------

add dict的排列:

1. 用 lambda

2. 用operator.itemgetter()

>>> xs = {'a': 4, 'b':3, 'c':2, 'd':1}

>>> sorted(xs.items(),key = lambda x:x[1])

[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

>>> import operator

>>> sorted(xs.items(), key=operator.itemgetter(1))

[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

>>> 

posted @ 2018-11-01 16:45  vivi~  阅读(1042)  评论(0编辑  收藏  举报