Home > Software design >  How to custom sort nested OrderedDict by list with different item sizes?
How to custom sort nested OrderedDict by list with different item sizes?

Time:05-16

I want to sort a custom list sort_by=['missed','stopped','prevented']. I want to sort var3 by that exact order, meaning it should try and get missed first, stopped second, etc. I have a data that looks like this:

OrderedDict([
        (
            "A1",
            [
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "missed",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
            ],
        ),
        (
            "A2",
            [
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "prevented",
                },
            ],
        ),
        (
            "A3",
            [
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "prevented",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "missed",
                },
            ],
        ),...
)

I am trying to achieve this:

OrderedDict([
        (
            "A1",
            [
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "missed",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
            ],
        ),
        (
            "A2",
            [
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "stopped",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "prevented",
                },
            ],
        ),
        (
            "A3",
            [
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "missed",
                },
                {
                    "var1": "xxxxx",
                    "var2": "xxxxx",
                    "var3": "prevented",
                },
            ],
        ),...
)

I was able to sort A1, A2, A3, but how do I go about with nesting and different sizing of A1, A2, and A3? This is the code I got to sort A1, A2, A3:

list_ordered = OrderedDict(sorted(t.items(), key=lambda item: sorted_by_title.index(item[0])))

CodePudding user response:

Assuming d the input OrderedDict, you can use:

sort_by=['missed','stopped','prevented']
# compute a sorter dictionary for efficiency
sorter = {w:i for i,w in enumerate(sort_by)}

OrderedDict((k, sorted(v, key=lambda x: sorter.get(x.get('var3')))) for k,v in d.items())

Output:

OrderedDict([('A1',
              [{'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'missed'},
               {'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'stopped'},
               {'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'stopped'},
               {'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'stopped'}]),
             ('A2',
              [{'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'stopped'},
               {'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'prevented'}]),
             ('A3',
              [{'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'missed'},
               {'var1': 'xxxxx', 'var2': 'xxxxx', 'var3': 'prevented'}])])
  • Related