【问题标题】:How to sort a complicated nested dictionary with respect to an attribute如何根据属性对复杂的嵌套字典进行排序
【发布时间】:2021-09-06 07:26:17
【问题描述】:

我的字典“d”本身包含一些分组的字典,其中一组如下..(根据“shelf_system”排序)

temp_var = [
    {
        "transform": {
            "header": {
                "seq": 0,
                "stamp": {
                    "secs": 1605597549,
                    "nsecs": 770021915
                },
                "frame_id": "map_footprint"
            },
            "child_frame_id": "6043ed310db7dfc3d3c41fb3",
            "transform": {
                "translation": {
                    "x": -1.2897373750100931,
                    "y": 13.525802678603945,
                    "z": 1.4157646948972575
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": -0.395544227727321,
                    "w": 0.918446930373006
                }
            }
        },
        "is_bottom": False,
        "depth": 0.45500001311302185,
        "width": 1.0,
        "shelf_system": "6043e59d74196bfc8f697394",
        "id": "6043ed310db7dfc3d3c41fb3"
    },
    {
        "transform": {
            "header": {
                "seq": 0,
                "stamp": {
                    "secs": 1605597549,
                    "nsecs": 770021915
                },
                "frame_id": "map_footprint"
            },
            "child_frame_id": "6043ed310db7dfc3d3c41fb4",
            "transform": {
                "translation": {
                    "x": -1.27337099143418,
                    "y": 13.541279684249133,
                    "z": 0.41059051072661096
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": -0.395544227727321,
                    "w": 0.918446930373006
                }
            }
        },
        "is_bottom": False,
        "depth": 0.45500001311302185,
        "width": 1.0,
        "shelf_system": "6043e59d74196bfc8f697394",
        "id": "6043ed310db7dfc3d3c41fb4"
    },
    {
        "transform": {
            "header": {
                "seq": 0,
                "stamp": {
                    "secs": 1605597549,
                    "nsecs": 770021915
                },
                "frame_id": "map_footprint"
            },
            "child_frame_id": "6043ed310db7dfc3d3c41fb5",
            "transform": {
                "translation": {
                    "x": -1.2775171778690428,
                    "y": 13.537358808842496,
                    "z": 0.660071025480387
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": -0.395544227727321,
                    "w": 0.918446930373006
                }
            }
        },
        "is_bottom": False,
        "depth": 0.45500001311302185,
        "width": 1.0,
        "shelf_system": "6043e59d74196bfc8f697394",
        "id": "6043ed310db7dfc3d3c41fb5"
    },
    {
        "transform": {
            "header": {
                "seq": 0,
                "stamp": {
                    "secs": 1605597549,
                    "nsecs": 770021915
                },
                "frame_id": "map_footprint"
            },
            "child_frame_id": "6043ed310db7dfc3d3c41fb6",
            "transform": {
                "translation": {
                    "x": -1.2877802993670282,
                    "y": 13.527653403330962,
                    "z": 1.1649424659690149
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": -0.395544227727321,
                    "w": 0.918446930373006
                }
            }
        },
        "is_bottom": False,
        "depth": 0.45500001311302185,
        "width": 1.0,
        "shelf_system": "6043e59d74196bfc8f697394",
        "id": "6043ed310db7dfc3d3c41fb6"
    },
    {
        "transform": {
            "header": {
                "seq": 0,
                "stamp": {
                    "secs": 1605597549,
                    "nsecs": 770021915
                },
                "frame_id": "map_footprint"
            },
            "child_frame_id": "6043ed310db7dfc3d3c41fb7",
            "transform": {
                "translation": {
                    "x": -1.357412898285477,
                    "y": 13.461804761720488,
                    "z": 0.1328539536965222
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": -0.395544227727321,
                    "w": 0.918446930373006
                }
            }
        },
        "is_bottom": True,
        "depth": 0.5550000071525574,
        "width": 1.0,
        "shelf_system": "6043e59d74196bfc8f697394",
        "id": "6043ed310db7dfc3d3c41fb7"
    },
    {
        "transform": {
            "header": {
                "seq": 0,
                "stamp": {
                    "secs": 1605597549,
                    "nsecs": 770021915
                },
                "frame_id": "map_footprint"
            },
            "child_frame_id": "6043ed310db7dfc3d3c41fb8",
            "transform": {
                "translation": {
                    "x": -1.2819964470466236,
                    "y": 13.5331229509924,
                    "z": 0.9068817640944433
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": -0.395544227727321,
                    "w": 0.918446930373006
                }
            }
        },
        "is_bottom": False,
        "depth": 0.45500001311302185,
        "width": 1.0,
        "shelf_system": "6043e59d74196bfc8f697394",
        "id": "6043ed310db7dfc3d3c41fb8"
    }
]

注意: 这只是一组,我的“d”有 158 个。

我想遍历每个组并按照属性“z”(高度)的递增顺序排列元素。

我该怎么做?

【问题讨论】:

标签: python arrays sorting dictionary


【解决方案1】:
def getZ(d):
    return d['transform']['transform']['translation']['z']

#define your dictionary
d_list = [{'transform': {'header': {'seq': 0, 'stamp': {'secs': 1605597549, 'nsecs': 770021915}, 'frame_id': 'map_footprint'}, 'child_frame_id': '6043ed310db7dfc3d3c41fb3', 'transform': {'translation': {'x': -1.2897373750100931, 'y': 13.525802678603945, 'z': 1.4157646948972575}, 'rotation': {'x': 0.0, 'y': 0.0, 'z': -0.395544227727321, 'w': 0.918446930373006}}}, 'is_bottom': False, 'depth': 0.45500001311302185, 'width': 1.0, 'shelf_system': '6043e59d74196bfc8f697394', 'id': '6043ed310db7dfc3d3c41fb3'},

{'transform': {'header': {'seq': 0, 'stamp': {'secs': 1605597549, 'nsecs': 770021915}, 'frame_id': 'map_footprint'}, 'child_frame_id': '6043ed310db7dfc3d3c41fb4', 'transform': {'translation': {'x': -1.27337099143418, 'y': 13.541279684249133, 'z': 0.41059051072661096}, 'rotation': {'x': 0.0, 'y': 0.0, 'z': -0.395544227727321, 'w': 0.918446930373006}}}, 'is_bottom': False, 'depth': 0.45500001311302185, 'width': 1.0, 'shelf_system': '6043e59d74196bfc8f697394', 'id': '6043ed310db7dfc3d3c41fb4'},

{'transform': {'header': {'seq': 0, 'stamp': {'secs': 1605597549, 'nsecs': 770021915}, 'frame_id': 'map_footprint'}, 'child_frame_id': '6043ed310db7dfc3d3c41fb5', 'transform': {'translation': {'x': -1.2775171778690428, 'y': 13.537358808842496, 'z': 0.660071025480387}, 'rotation': {'x': 0.0, 'y': 0.0, 'z': -0.395544227727321, 'w': 0.918446930373006}}}, 'is_bottom': False, 'depth': 0.45500001311302185, 'width': 1.0, 'shelf_system': '6043e59d74196bfc8f697394', 'id': '6043ed310db7dfc3d3c41fb5'},

{'transform': {'header': {'seq': 0, 'stamp': {'secs': 1605597549, 'nsecs': 770021915}, 'frame_id': 'map_footprint'}, 'child_frame_id': '6043ed310db7dfc3d3c41fb6', 'transform': {'translation': {'x': -1.2877802993670282, 'y': 13.527653403330962, 'z': 1.1649424659690149}, 'rotation': {'x': 0.0, 'y': 0.0, 'z': -0.395544227727321, 'w': 0.918446930373006}}}, 'is_bottom': False, 'depth': 0.45500001311302185, 'width': 1.0, 'shelf_system': '6043e59d74196bfc8f697394', 'id': '6043ed310db7dfc3d3c41fb6'},

{'transform': {'header': {'seq': 0, 'stamp': {'secs': 1605597549, 'nsecs': 770021915}, 'frame_id': 'map_footprint'}, 'child_frame_id': '6043ed310db7dfc3d3c41fb7', 'transform': {'translation': {'x': -1.357412898285477, 'y': 13.461804761720488, 'z': 0.1328539536965222}, 'rotation': {'x': 0.0, 'y': 0.0, 'z': -0.395544227727321, 'w': 0.918446930373006}}}, 'is_bottom': True, 'depth': 0.5550000071525574, 'width': 1.0, 'shelf_system': '6043e59d74196bfc8f697394', 'id': '6043ed310db7dfc3d3c41fb7'},

{'transform': {'header': {'seq': 0, 'stamp': {'secs': 1605597549, 'nsecs': 770021915}, 'frame_id': 'map_footprint'}, 'child_frame_id': '6043ed310db7dfc3d3c41fb8', 'transform': {'translation': {'x': -1.2819964470466236, 'y': 13.5331229509924, 'z': 0.9068817640944433}, 'rotation': {'x': 0.0, 'y': 0.0, 'z': -0.395544227727321, 'w': 0.918446930373006}}}, 'is_bottom': False, 'depth': 0.45500001311302185, 'width': 1.0, 'shelf_system': '6043e59d74196bfc8f697394', 'id': '6043ed310db7dfc3d3c41fb8'}]

d_list.sort(key=getZ)

这将为您提供按“翻译”中的值 z 排序的 d_list

【讨论】:

    【解决方案2】:

    考虑到您的变量名称是temp_var。您有两个地方提到了密钥 z。

    基于键translation中的键z进行排序

    # ascending
    temp_var.sort(key=lambda x:x['transform']['transform']['translation']['z'])
    # descending
    temp_var.sort(key=lambda x:x['transform']['transform']['translation']['z'], reverse=True)
    

    用于根据键rotation中存在的键z进行排序

    temp_var.sort(key=lambda x:x['transform']['rotation']['translation']['z'])
    

    【讨论】:

    • 请改用get。否则会报错。
    • 是的,上面的代码假设数组中的所有dict对象都有相似的key-value对
    • 您能否详细说明您的意见? @Vishnudev
    • 当我收到错误“dict has not attribute sort”
    • @RidaSagheer temp_var 变量应该是 list 在您的帖子中。在你的情况下,它是一本字典,因此是错误的。
    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2021-12-24
    • 2012-08-03
    • 2021-05-27
    • 2023-03-14
    相关资源
    最近更新 更多