OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to properly sort a nested dictionary by keys in Python3, where we take in account numbers from key strings

  • Thread starter Thread starter user25681348
  • Start date Start date
U

user25681348

Guest
Assuming a dictionary that looks like this, where we have a string with some number for the key (not necessarily in any order, nor is it guaranteed that it would be in a continues sequence, and the only pattern that the keys would follow is that the random key string would have a number at the end of the string after a -):

Code:
nested_dict = {
    "outter_key_layer-3": {
        "inner_key_layer-4": "random_value",
        "inner_key_layer-2": "random_value",
        ...
        "inner_key_layer-11": "random_value",
        "inner_key_layer-22": "random_value",
        ...
        ...
        "inner_key_layer-31": "random_value",
        "inner_key_layer-112": "random_value",
        ...
    },
    "outter_key_layer-54": {
        "inner_key_layer-1": "random_value",
        "inner_key_layer-2": "random_value",
        ...
        "inner_key_layer-11": "random_value",
        "inner_key_layer-112": "random_value",
        ...
        "inner_key_layer-12": "random_value",
        "inner_key_layer-11": "random_value",
        ...
    },
    ...
    "outter_key_layer-13": {
        "inner_key_layer-1": "random_value",
        "inner_key_layer-2": "random_value",
        ...
        "inner_key_layer-5": "random_value",
        "inner_key_layer-10": "random_value",
        ...
        "inner_key_layer-6": "random_value",
        "inner_key_layer-23": "random_value",
        ...
    },
    ...
}

How could I achieve the following, where the keys are both in alphabetical order, but the numbers are also taken into consideration as integers for sorting:

Code:
nested_dict = {
    "outter_key_layer-1": {
        "inner_key_layer-1": "random_value",
        "inner_key_layer-2": "random_value",
        ...
        "inner_key_layer-11": "random_value",
        "inner_key_layer-12": "random_value",
        ...
        ...
        "inner_key_layer-111": "random_value",
        "inner_key_layer-112": "random_value",
        ...
    },
    "outter_key_layer-2": {
        "inner_key_layer-1": "random_value",
        "inner_key_layer-2": "random_value",
        ...
        "inner_key_layer-11": "random_value",
        "inner_key_layer-12": "random_value",
        ...
        "inner_key_layer-111": "random_value",
        "inner_key_layer-112": "random_value",
        ...
    },
    ...
    "outter_key_layer-11": {
        "inner_key_layer-1": "random_value",
        "inner_key_layer-2": "random_value",
        ...
        "inner_key_layer-11": "random_value",
        "inner_key_layer-12": "random_value",
        ...
        "inner_key_layer-111": "random_value",
        "inner_key_layer-112": "random_value",
        ...
    },
    ...
}

Attempted to modify the following from a previously asked question, but I can't get anything to work like attempting to filter out the integers and get them sorted somehow:

Code:
sorted_dict = {key: dict(sorted(nested_dict[key].items())) for key in sorted(nested_dict)}

With the above the nested inner keys, wouldn't be sorted as expected, the numeric values would still be taken as strings: inner_key_layer-1, inner_key_layer-11, ..., inner_key_layer-12, inner_key_layer-2...

I know there are a lot of different use case questions related to sorting things with python, but any help or suggestions would be welcomed. Thank you.

LE: After doing some further research I stumbled upon the natsort package, and using it this way basically yields what I'm looking for:

Code:
from natsort import natsorted


my_sorted_dict = {key: dict(natsorted(nested_dict[key].items())) for key in sorted(nested_dict)}

However I'm still wondering if there's a way of doing this without using or having to install another package.
<p>Assuming a dictionary that looks like this, where we have a string with some number for the key (not necessarily in any order, nor is it guaranteed that it would be in a continues sequence, and the only pattern that the keys would follow is that the random key string would have a number at the end of the string after a <code>-</code>):</p>
<pre><code>nested_dict = {
"outter_key_layer-3": {
"inner_key_layer-4": "random_value",
"inner_key_layer-2": "random_value",
...
"inner_key_layer-11": "random_value",
"inner_key_layer-22": "random_value",
...
...
"inner_key_layer-31": "random_value",
"inner_key_layer-112": "random_value",
...
},
"outter_key_layer-54": {
"inner_key_layer-1": "random_value",
"inner_key_layer-2": "random_value",
...
"inner_key_layer-11": "random_value",
"inner_key_layer-112": "random_value",
...
"inner_key_layer-12": "random_value",
"inner_key_layer-11": "random_value",
...
},
...
"outter_key_layer-13": {
"inner_key_layer-1": "random_value",
"inner_key_layer-2": "random_value",
...
"inner_key_layer-5": "random_value",
"inner_key_layer-10": "random_value",
...
"inner_key_layer-6": "random_value",
"inner_key_layer-23": "random_value",
...
},
...
}
</code></pre>
<p>How could I achieve the following, where the keys are both in alphabetical order, but the numbers are also taken into consideration as integers for sorting:</p>
<pre><code>nested_dict = {
"outter_key_layer-1": {
"inner_key_layer-1": "random_value",
"inner_key_layer-2": "random_value",
...
"inner_key_layer-11": "random_value",
"inner_key_layer-12": "random_value",
...
...
"inner_key_layer-111": "random_value",
"inner_key_layer-112": "random_value",
...
},
"outter_key_layer-2": {
"inner_key_layer-1": "random_value",
"inner_key_layer-2": "random_value",
...
"inner_key_layer-11": "random_value",
"inner_key_layer-12": "random_value",
...
"inner_key_layer-111": "random_value",
"inner_key_layer-112": "random_value",
...
},
...
"outter_key_layer-11": {
"inner_key_layer-1": "random_value",
"inner_key_layer-2": "random_value",
...
"inner_key_layer-11": "random_value",
"inner_key_layer-12": "random_value",
...
"inner_key_layer-111": "random_value",
"inner_key_layer-112": "random_value",
...
},
...
}
</code></pre>
<p>Attempted to modify the following from a <a href="https://stackoverflow.com/questions/72671829/how-to-sort-nested-dictionaries-in-python">previously asked question</a>, but I can't get anything to work like attempting to filter out the integers and get them sorted somehow:</p>
<pre><code>sorted_dict = {key: dict(sorted(nested_dict[key].items())) for key in sorted(nested_dict)}
</code></pre>
<p>With the above the nested inner keys, wouldn't be sorted as expected, the numeric values would still be taken as strings: <code>inner_key_layer-1</code>, <code>inner_key_layer-11</code>, ..., <code>inner_key_layer-12</code>, <code>inner_key_layer-2</code>...</p>
<p>I know there are a lot of different use case questions related to sorting things with python, but any help or suggestions would be welcomed. Thank you.</p>
<p><strong>LE</strong>: After doing some further research I stumbled upon the <code>natsort</code> package, and using it this way basically yields what I'm looking for:</p>
<pre><code>from natsort import natsorted


my_sorted_dict = {key: dict(natsorted(nested_dict[key].items())) for key in sorted(nested_dict)}
</code></pre>
<p>However I'm still wondering if there's a way of doing this without using or having to install another package.</p>
 

Latest posts

H
Replies
0
Views
1
Hür Doğan ÜNLÜ
H
Top