简体   繁体   中英

Iterate through a sorted nested dictionary in python

I'm facing troubles to iterate through a sorted nested dictionary. By nested dictionary, I mean a dictionary that contains other dictionaries like this :

{"A": 
    {"subA": {"Date": ["date1", "date2"],
              "numDownloads": [int1, int2]}
     "subB": {"Date": ["date3", "date4"],
              "numDownloads": [int3, int4]}
    }
 "B":
    {"subC": {"Date": ["date5", "date6"],
              "numDownloads": [int5, int6]}
     "subD": {"Date": ["date7", "date8"],
              "numDownloads": [int7, int8]}
    }
}

I would like to iterate for each first-level keys (A and B in this case) on the second-level keys (subA, subB for A and subC, subD for B) sorted by the first value of numDownloads ascending (int1, int 3 for A and int 5, int 7 for B).

If I give an example of what I want : Consider this dictionnary :

{"A": 
    {"subA": {"Date": ["date1", "date2"],
              "numDownloads": [100, 500]}
     "subB": {"Date": ["date3", "date4"],
              "numDownloads": [500, 1000]}
    }
 "B":
    {"subC": {"Date": ["date5", "date6"],
              "numDownloads": [10000, 50000]}
     "subD": {"Date": ["date7", "date8"],
              "numDownloads": [500, 1000]}
    }
}

I would like to iterate though A with subA then subB as subA first numDownloads value (100) is lower than subB first numDownloads value (500) and though B with subD then subC and subC first numDownloads value (10000) is greater than subD first numDownloads value (500).

I tried with the sorted() function and a lambda expression but I can't make it work for my case.

for firstKey in myDict:
    firstKeyData = myDict[firstKey]
    for key in sorted(firstKeyData, key=lambda k: firstKeyData[k]["numDownloads"][0]):
        // here, sub keys are sorted according to the first element of numDownloads 
        // A : subA then subB
        // B : subD then subC

Here is the example of the json file :

{
   "Social":{
      "Facebook":{
         "Date":[
            "2019-02-19"
         ],
         "numDownloads":[
            "100000000"
         ]
      },
      "Twitter":{
         "Date":[
            "2019-02-19"
         ],
         "numDownloads":[
            "100000"
         ]
      }
   },
   "Instagram":{
      "Date":[
         "2019-02-19"
      ],
      "numDownloads":[
         "5000"
      ]
   },
   "Communication":{
      "Messenger":{
         "Date":[
            "2019-02-19"
         ],
         "numDownloads":[
            "100000"
         ]
      },
      "Whatsapp":{
         "Date":[
            "2019-02-19"
         ],
         "numDownloads":[
            "50000000"
         ]
      }
   }
}

Thanks for all

The numDownloads keys of your actual JSON file contain strings, rather than integers as suggested by the simplified version of your sample data, so comparisons are made lexicographically rather than numerically when you make that the returning value of your key function for sorting, so "20" would be considered "greater" than "100" as a result, for example.

You should convert the referenced items to integers instead:

for key in sorted(firstKeyData, key=lambda k: int(firstKeyData[k]["numDownloads"][0])):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM