Python: Sorting dictionary base on specific string in key [duplicate]

289
July 28, 2017, at 09:26 AM

This question already has an answer here:

  • How can I sort a dictionary by key? 20 answers
  • How to sort dictionaries by keys in Python 6 answers

I'm new to Python and I want to sort a dictionary base on specific group of numbers in key. This is what I have:

{
    "1.1.17583.0": {
        "value": "true"
    },
    "1.1.17585.0": {
        "value": "true"
    },
    "1.10.28386.0": {
        "value": "true"
    },
    "1.10.28410.0": {
        "value":"true"
    },
    "1.2.17586.0": {
        "value": "true"
    }
}

I want to sort the dictionary based on third group number value (17583, 17585, ...) of key. So, the final sorted dictionary should be like this:

{
    "1.1.17583.0": {
        "value": "true"
    },
    "1.1.17585.0": {
        "value": "true"
    },
    "1.2.17586.0": {
        "value": "true"
    },
    "1.10.28386.0": {
        "value": "true"
    },
    "1.10.28410.0": {
        "value":"true"
    }
}

Is there a way to do this? Thank you in advance!

Edited: As this was my first question here, I found that some people forgot how is at the beginning. Anyway, I've got a good point from @Artyer, and this seems to work fine:

import json
from collections import OrderedDict
with open("myfile.json") as data_file:
    data = json.load(data_file, )
for item in OrderedDict(sorted(data.items(), key=lambda t: t[0].split('.')[2])):
    print item

It will output:

1.1.17583.0
1.1.17585.0
1.2.17586.0
1.10.28386.0
1.10.28410.0
Rent Charter Buses Company
READ ALSO
Invalid syntax for turtle name (python)

Invalid syntax for turtle name (python)

I am trying to write a program to draw a flower, but no matter what I do it keeps throwing an "invalid syntax" error for the turtle nameI have taken out all of my other code, tried naming the turtle something different, yet nothing works

543
How to disable unit testing for Connect SDK Lite on Android project?

How to disable unit testing for Connect SDK Lite on Android project?

I have added Connect SDK Lite from sources to my Android project and am looking to use it to stream to Apple TVI chose to use it from sources because my project already has a bunch of libraries from sources and the dependencies for Connect SDK were conflicting...

358
Is it possible to hold a ScrollView to be always scrollable?

Is it possible to hold a ScrollView to be always scrollable?

I want to make my Scrollview always scrollable even though it has no element in it

271
Best practices for Android Git branching

Best practices for Android Git branching

Help settle an argument,

351