0

my dictionary "d" contains some grouped dict within itself, out of which one group is as below.. (sorted according to "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"
    }
]

Note: This is only one group, my "d" has 158 of them.

I want to iterate over each group and arrange the elements with increasing order of attribute "z" (height).

How can i do that?

Amit Pathak
  • 1,145
  • 11
  • 26

2 Answers2

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)

This will give you d_list sorted by the value z in 'translation'

Ben Alan
  • 1,399
  • 1
  • 11
  • 27
0

Considering your variable name is temp_var. You have two places where key z is mentioned.

For sorting on the basis of key z present in key translation

# 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)

For sorting on the basis of key z present in key rotation

temp_var.sort(key=lambda x:x['transform']['rotation']['translation']['z'])
Amit Pathak
  • 1,145
  • 11
  • 26