Searching Dictionary
Support Desk
support.desk.ipg at gmail.com
Wed Sep 23 17:28:28 EDT 2009
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')
would return
{'252': [{'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield':
'252', 'from': '9876662881', 'to': '19877760406', 'fpld': '"Brochsteins"
<9855562881>', 'result': 'ANSWERED', 'billsec': 131}, {'code': '51679',
'date': '2009-08-01 14:33:55', 'userfield': '252', 'from': '9876662881',
'to': '19877770391', 'fpld': '"Brochsteins" <9876555881>', 'result':
'ANSWERED', 'billsec': 86}]}
I hope this makes sense, sorry for not being clear
On Wed, Sep 23, 2009 at 3:34 PM, Simon Forman <sajmikins at gmail.com> wrote:
> On Wed, Sep 23, 2009 at 2:31 PM, Support Desk
> <support.desk.ipg at gmail.com> wrote:
> >
> > i am trying to search a large Python dictionary for a matching value. The
> > results would need to be structured into a new dictionary with the same
> > structure. Thanks.
> >
> > The structure is like this
> >
> > { Key : [{'item':value,'item2':value,'
> >
> item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}],
> > Key2 :
> >
> [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}],
> > Key3 :
> >
> [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]
> > }
>
> There is not really enough information to answer your query directly.
> But I'll take a guess anyway.
>
>
> First, let's put your in a piece of code that will actually run (which
> is what you shourd have done in the first place..):
>
>
>
> Key = Key2 = Key3 = value = 42
>
>
> D = {
> Key: [
> {'item':value,
> 'item2':value,
> 'item3':123,
> 'item4':value,
> 'item5':value,
> 'item6':value,
> 'item7':value,
> 'item8':value,
> 'item9':value}
> ],
> Key2: [
> {'item':value,
> 'item2':value,
> 'item3':value,
> 'item4':value,
> 'item5':value,
> 'item6':value,
> 'item7':value,
> 'item8':value,
> 'item9':value}
> ],
> Key3: [
> {'item':value,
> 'item2':value,
> 'item3':123,
> 'item4':value,
> 'item5':value,
> 'item6':value,
> 'item7':value,
> 'item8':value,
> 'item9':value}
> ]
> }
>
> Now let's write a function that (maybe) does what you (kind of) specified:
>
> def wtf(d, match_me):
> result = {}
> for key, value in d.iteritems():
> inner_dict = value[0]
> for inner_key, inner_value in inner_dict.iteritems():
> if inner_value == match_me:
> result[key] = [{inner_key: inner_value}]
> return result
>
>
> And let's try it:
>
> print wtf(D, 42)
>
> That prints:
>
> {42: [{'item': 42}]}
>
>
> Is that what you're asking for?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090923/48ca26cb/attachment.html>
More information about the Python-list
mailing list