繁体   English   中英

按字典值对字典的嵌套列表进行排序

[英]Sort nested list of dict by it's dict value

我有以下结构:

d = {
    'futures': {
        'test': {
            'nested': {
                1: {
                    'list': [
                             {
                                 'c': 'third',
                                 'price': 3
                             },
                             {
                                 'b': 'second',
                                 'price': 2
                             },
                             {
                                 'a': 'first',
                                 'price': 1
                             }                             
                    ]
                },
                2: {
                    'list': [
                             {
                                 'f': 'sixth',
                                 'price': 6
                             },
                             {
                                 'e': 'fifth',
                                 'price': 5
                             },
                             {
                                 'd': 'fourth',
                                 'price': 4
                             }                             
                    ]
                }
            }
        }
    }
}

我需要按价格对每个列表进行排序,升序。 结果应该是:

d = {
    'futures': {
        'test': {
            'nested': {
                1: {
                    'list': [
                             {
                                 'a': 'first',
                                 'price': 1
                             },
                             {
                                 'b': 'second',
                                 'price': 2
                             },
                             {
                                 'c': 'third',
                                 'price': 3
                             },                            
                                                         
                    ]
                },
                2: {
                    'list': [
                             {
                                 'd': 'fourth',
                                 'price': 4
                             },
                             {
                                 'e': 'fifth',
                                 'price': 5
                             },
                             {
                                 'f': 'sixth',
                                 'price': 6
                             }                             
                                                         
                    ]
                }
            }
        }
    }
}

由于这种特殊的结构,我发现的所有问题都不适合我的需求。

有没有办法订购它而不必访问每个以前的键? 因为在我的项目中,我在列表之前有更多嵌套键的案例,所以我需要一个动态解决方案来对其进行排序。 我的意思是,我不知道列表的确切路径,只知道列表键。

制作一个 function 递归遍历您的dict以查找列表,并根据您的标准对每个列表进行排序:

def find_and_sort_lists(d):

    for value in d.values():

        if isinstance(value, list):
            value.sort(key = lambda nested_d: nested_d['price'])

        if isinstance(value, dict):
            find_and_sort_lists(value)

如果需要仅对键实际上是'list'进行排序,则可以使用以下命令:

def find_and_sort_lists(d):

    for key, value in d.items():

        if key == 'list' and isinstance(value, list):
            value.sort(key = lambda nested_d: nested_d['price'])

        if isinstance(value, dict):
            find_and_sort_lists(value)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM