Search code examples
pythonlistpython-2.7dictionaryextend

how to extend a python dictionary while there are list inside


I have tried to fetch data from a dictionary like mongodb do.

while there is a document like

{'a': 'b', 'c': ['d', {'e': ['i', {'j': 'h'}]}]}

that I want to get the value 'h' from the search string like 'c.e.j' as mongodb will do

> db.cc.findOne({'c.e.j':'h'})
{
    "_id" : ObjectId("551e5047342b12656b4edecc"),
    "a" : "b",
    "c" : [
        "d",
        {
            "e" : [
                "i",
                {
                    "j" : "h"
                }
            ]
        }
    ]
}

so the first time I think is I will need to extend the dictionary for each list inside, and I have been coding for 4 hours, buy I think it is hard for me.

The final version of my code looks like this:

import types

def extdic(cc):
    lst = []
    for key in cc:
        value = cc[key]
        if type(value) == types.DictType:
            for _ in extdic(value):
                cc[key] = _
                lst.append(eval(repr(cc)))
            return lst
        if type(value) == types.ListType:
            #print cc
            for _ in value:
                cc[key] = _
                lst.append(eval(repr(cc)))
            #print lst
            return lst
        else:
            return [cc]

def mkdic(cc):
    lst = []
    if type(cc) == types.ListType:
        lst = cc
    else:
        lst = [cc]

    reslst = []
    while True:
        for _ in lst:
            #print _
            ext = extdic(_)
            #print ext
            reslst = reslst + ext
        if len(reslst) == len(lst):
            break
        else:
            lst = reslst
            reslst = []
    return lst

if __name__ == '__main__':
    cc = [
        {'a': 'b', 'c': ['d', {'e': ['i', 'j']}]}, 
        {'f':['g','h']}
    ]

    cd = {'a': 'b', 'c': {'e': ['i', 'j']}}

    ce = {'a': {'b': {'c':{'d':{'e':['f','g']}}}}}
    for _ in mkdic(cc):
        print _

It is sad that I still cannot get what I want

I only get the 'ce' dictionary work like

MBA:code cc$ python todic3.py 
{'a': {'b': {'c': {'d': {'e': 'f'}}}}}
{'a': {'b': {'c': {'d': {'e': 'g'}}}}}
MBA:code cc$ 

other dictionary struct still not the thing I want..

MBA:code cc$ python todic3.py 
{'a': 'b', 'c': ['d', {'e': ['i', {'j': 'h'}]}]}
{'f': 'g'}
{'f': 'h'}
MBA:code cc$ 

I want to use the tools like

MBA:code cc$ echo "{'a': 'b', 'c': ['d', {'e': ['i', {'j': 'h'}]}]}" | python todic3.py c.e.j
c.e.j: h
MBA:code cc$  

help please..

thank you very much


Solution

  • This should return the specified value. The 'yield from' statements require that you use Python 3. Unfortunately I cannot test it ATM. EDIT: I tested it just now, turns out there where some significant bugs. I fixed them and it works as intended now.

    def iter_flattened(data):
        '''returns a generator that allows iteration over key,value pairs, even if the dictionaries are nested in lists'''
        if isinstance(data, dict):
            yield from data.items()
        elif isinstance(data, list):      
            for item in data:
                yield from iter_flattened(item)
    
    def _find(data, keys):
        '''This function searches the given (sub-)tree for the given keys'''
        if len(keys) == 0:
            return data
    
        for key, value in iter_flattened(data):
            if key == keys[0]:
                result = _find(value, keys[1:])
            else:
                result = _find(value, keys)
            if result is not None:
                return result
        return None
    
    def find(data, path):
        '''Interface function, that accepts the keys as a string seperated with dots.'''
        keys = path.split('.')
        return _find(data, keys)
    

    To be used like this:

    data = {'a': 'b', 'c': ['d', {'e': ['i', {'j': 'h'}]}]}
    value = find(data, 'c.e.j')
    print(value) # --> h