Searching a large dictionary

Martien Verbruggen martien.verbruggen at invalid.see.sig
Wed Sep 23 20:21:04 EDT 2009


On Wed, 23 Sep 2009 14:52:56 -0700 (PDT),
	mike171562 <support.desk.ipg at gmail.com> wrote:
> Sorry for the confusion, Simon, this is almost exactly what I need,
> but i need to be able to search for a string in a given value of an
> item
>
> Here is an example of the dict I am working with
>
> {'252': [{'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield':
> '252', 'from': '9876662881', 'to': '19877760406', 'fpld': '"Foobar"
><9855562881>', 'result': 'ANSW', 'sec': 131}, {'code': '51679',
> 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from':
> '9876662881', 'to': '19877770391', 'fpld': '"Foobar" <9876555881>',
> 'result': 'ANSW', 'sec': 86}]}
>
>
> 252 being the key, I need to be able to search for a string in a given
> item , say 777 in the 'to' field so
>
> print wtf(dict,'to','777')

And where in this do you tell wtf() that you want to look at 252 only?
>From your other posts it seems that the above is actually part of a
larger dict with other keys besides 252. Or do you want all of those
returned? I'll assume you want to look through ever key at the 'top'
level, and through every dict in the list that's associated with that.

> would return
>
> {'252': [{'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield':
> '252', 'from': '9876662881', 'to': '19877760406', 'fpld': '"Foobar"
><9855562881>', 'result': 'ANSW', 'billsec': 131}, {'code': '51679',
> 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from':
> '9876662881', 'to': '19877770391', 'fpld': '"Foobar" <9876555881>',
> 'result': 'ANSW', 'billsec': 86}]}
>
> I hope this makes sense, sorry for not being clear

Did you mean something like:

top_dict = {
    '252': [
        {'code': '51679', 
            'date': '2009-08-01 11:35:38', 
            'userfield': '252', 
            'from': '9876662881', 
            'to': '19877760406', 
            'fpld': '"Foobar" <9855562881>', 
            'result': 'ANSW', 
            'sec': 131}, 
        {'code': '51679',
            'date': '2009-08-01 14:33:55', 
            'userfield': '252', 
            'from': '9876662881', 
            'to': '19877770391', 
            'fpld': '"Foobar" <9876555881>', 
            'result': 'ANSW', 'sec': 86},
        {'code': 'foo',
            'date': '2009-08-01 14:33:55', 
            'userfield': '252', 
            'from': '9876662881', 
            'to': '198770391', 
            'fpld': '"Foobar" <9876555881>', 
            'result': 'ANSW', 'sec': 86}
        ],
    '253': [
        {'code': '51679', 
            'date': '2009-08-01 11:35:38', 
            'userfield': '252', 
            'from': '9876662881', 
            'to': '19877760406', 
            'fpld': '"Foobar" <9855562881>', 
            'result': 'ANSW', 
            'sec': 131}, 
        {'code': 'foo',
            'date': '2009-08-01 14:33:55', 
            'userfield': '252', 
            'from': '9876662881', 
            'to': '198770391', 
            'fpld': '"Foobar" <9876555881>', 
            'result': 'ANSW', 'sec': 86}
    ]}

def wtf(top_dict, field, value):
    new_dict = {}
    for key, dict_list in top_dict.iteritems():
        for d in dict_list:
            if field in d and value in d[field]:
                if key not in new_dict:
                    new_dict[key] = []
                new_dict[key].append(d)
    return new_dict

nd = wtf(top_dict, 'fto', '777')
print nd

Martien
-- 
                             | 
Martien Verbruggen           | That's funny, that plane's dustin'
first.last at heliotrope.com.au | crops where there ain't no crops.
                             | 



More information about the Python-list mailing list