Parsing Python dictionary with multiple objects

Dave Angel davea at davea.name
Wed Oct 15 13:43:58 EDT 2014


Anurag Patibandla <anuragpatibandla7 at gmail.com> Wrote in message:
> Thanks for the response.
> Here is the code that I have tried.
> 
> from operator import itemgetter
> keys = json.keys()
> order = list(keys)
> q1 = int(round(len(keys)*0.2))
> q2 = int(round(len(keys)*0.3))
> q3 = int(round(len(keys)*0.5))
> b = [q1,q2,q3]
> n=0

threedicts = []

> for i in b:
>     queues = order[n:n+i]
>         
>     n = n+i
>     print queues
>     
>     for j in range(len(queues)):
>         q = (queues[j], json.get(queues[j]))
>         print q
> 

       onedict = {}
       for q in queues:
           onedict[q] = json[q]
       threedicts.append (onedict)
   dict1, dictw, dict3 = threedicts 
           
> By this I am able to get the 3 smaller dicts I want, but can you help me assign them to 3 variables?
> The dicts need not be ordered but it would be better if they are ordered.
> 

dicts are not ordered. If you want the items in a particular
 order, you have to do that after extraction from the dict. There
 is a related type called collections.OrderedDict, which
 'remembers' the order things were added.

-- 
DaveA




More information about the Python-list mailing list