python 字典嵌套字典排序

python字典字典的排序

import collections
import json
a = {2: {'name': u"beijing", 'pass_num': 42, 'pass_rate': u'12%',

         'pass_sort': 0.12},
     3: {'name': u"shanghai", 'pass_num': 50, 'pass_rate': u'22%',

         'pass_sort': 0.22},
     5: {'name': u"shenzheng", 'pass_num': 60, 'pass_rate': u'23%',

         'pass_sort': 0.23},

     10: {'name': u"hangzhou", 'pass_num': 42, 'pass_rate': u'42%',

          'pass_sort': 0.42},
     11: {'name': u"nanjing", 'pass_num': 45, 'pass_rate': u'45%',

          'pass_sort': 0.45}
     }


def get_dit_to_dit_sorted(info_dict, keys, reverse=True):
    """
    字典嵌套字典
    :param info_dict: 字典
    :param keys: 进行排序的key值
    :param reverse: 排序
    :return: 
    """""
    b = collections.OrderedDict((sorted(info_dict.items(), key=lambda x: x[1][keys], reverse=reverse)))
    return b


def main():
    b = get_dit_to_dit_sorted(a, "pass_sort")
    print(json.dumps(b))

你可能感兴趣的:(字典嵌套字典排序,python字典嵌套字典排序,返回有序字典)