Python 字典 将字典进行倒序翻转
>>> dic = {"a":1,"c":2,"b":3} >>> dic {'a': 1, 'c': 2, 'b': 3} >>> keys = list(dic.keys()) >>> values = list(dic.values()) >>> keys.reverse() >>> values.reverse() >>> dict(zip(keys,values)) {'b': 3, 'c': 2, 'a': 1}
暂时想到这个方法,有点繁琐,后面有想到更优的方法再来优化。
如果你想到了,欢迎评论告诉我喔!
Python 字典 按key值大小 将字典进行从大到小排序
>>> new_dic = {} >>> dic = {"a":1,"c":2,"b":3} >>> for key in sorted(dic.keys(),reverse=True): new_dic[key]=dic.get(key) >>> new_dic {'c': 2, 'b': 3, 'a': 1} >>>