Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Searching a large dictionary

Reply
Thread Tools

Re: Searching a large dictionary

 
 
Dave Angel
Guest
Posts: n/a
 
      09-23-2009
Support Desk wrote:
> I need help searching a large python dictionary. The dictionary is setup
> like so
>
> 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}]
>
>
>
> What would be the best way to search for a specific value of item1 and add
> all the results to a new dictionary? Thanks
>
>

With all the messages I've seen so far on this thread, all you've
managed to do is confuse it further. I suggest you start over (new
thread) with a legal program defining a single dictionary (with real
values for Key, Key2, and value, and without extra quote marks which
make it fail to compile even if we guess what you're doing with the
three keys). I suggest that at least some relevant parts of the data
should not be all identical, so we can tell what's important.

Use copy & paste from working code.

Then given a real dictionary, describe what you really want to search
for, and what acceptable results would be.

And also specify the programming language, version, and OS environment.

Thanks.
DaveA
 
Reply With Quote
 
 
 
 
mike171562
Guest
Posts: n/a
 
      09-23-2009
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}]
}




On Sep 22, 7:16*pm, Dave Angel <(E-Mail Removed)> wrote:
> Support Desk wrote:
> > I need help searching a large python dictionary. The dictionary is setup
> > like so

>
> > 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}]

>
> > What would be the best way to search for a specific value of item1 and add
> > all the results to a new dictionary? Thanks

>
> With all the messages I've seen so far on this thread, all you've
> managed to do is confuse it further. * I suggest you start over (new
> thread) with a legal program defining a single dictionary (with real
> values for Key, Key2, and value, and without extra quote marks which
> make it fail to compile even if we guess what you're doing with the
> three keys). *I suggest that at least some relevant parts of the data
> should not be all identical, so we can tell what's important.
>
> Use copy & paste from working code.
>
> Then given a real dictionary, describe what you really want to search
> for, and what acceptable results would be.
>
> And also specify the programming language, version, and OS environment.
>
> Thanks.
> DaveA


 
Reply With Quote
 
 
 
 
mike171562
Guest
Posts: n/a
 
      09-23-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': '"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







On Sep 23, 2:57*pm, mike171562 <(E-Mail Removed)> 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,'item 8':value,'item9':value}],
> Key2 :
> [{'item':value,'item2':value,'item3':value,'item4': value,'item5':value','it em6':value,'item7':value,'item8':value,'item9':val ue}],
> Key3 :
> [{'item':value,'item2':value,'item3':value,'item4': value,'item5':value','it em6':value,'item7':value,'item8':value,'item9':val ue}]
>
> }
>
> On Sep 22, 7:16*pm, Dave Angel <(E-Mail Removed)> wrote:
>
>
>
> > Support Desk wrote:
> > > I need help searching a large python dictionary. The dictionary is setup
> > > like so

>
> > > 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}]

>
> > > What would be the best way to search for a specific value of item1 and add
> > > all the results to a new dictionary? Thanks

>
> > With all the messages I've seen so far on this thread, all you've
> > managed to do is confuse it further. * I suggest you start over (new
> > thread) with a legal program defining a single dictionary (with real
> > values for Key, Key2, and value, and without extra quote marks which
> > make it fail to compile even if we guess what you're doing with the
> > three keys). *I suggest that at least some relevant parts of the data
> > should not be all identical, so we can tell what's important.

>
> > Use copy & paste from working code.

>
> > Then given a real dictionary, describe what you really want to search
> > for, and what acceptable results would be.

>
> > And also specify the programming language, version, and OS environment.

>
> > Thanks.
> > DaveA


 
Reply With Quote
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-24-2009
On Sep 23, 4:05*pm, "Rhodri James" <(E-Mail Removed)>
wrote:
> On Wed, 23 Sep 2009 22:52:56 +0100, mike171562 *
>
> <(E-Mail Removed)> 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}]}

>
> Ugh. *This isn't, you'll notice, either of the versions of the structure
> that you told us it was. *Let's prettyprint it for added clarity:
>
> { '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
> * * * * * * }
> * * * * * ]
>
> }
>
> A dictionary containing a single entry, whose value is a list of
> dictionaries.
>
>
>
> > 252 being the key,

>
> This vaguely implies that 252 (or rather '252') is the *only*
> key. *I hope that's not true, for it would be a silly dictionary.
>
> > 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': '"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}]}

>
> Which would be the entire original structure, except for some reason
> relabelling 'sec' as 'billsec' telling us very little really.
>
>
>
> > I hope this makes sense, sorry for not being clear

>
> Not a lot, frankly. *There are still a lot of unanswered questions here..
> Would I be right in thinking that you want to check all keys of the outer
> dictionary, and you want the corresponding values in the returned
> dictionary to be lists of all inner dictionaries that match? *And that
> the business with sec/billsec is a typo?
>
> Assuming that, try this:
>
> # Untested, and it is midnight
> def wtf(data_dict, key, value):
> * *# Don't call it 'dict', by the way, you'll mask the builtin object
> * *result = {}
> * *for outer_key, data_list in data_dict.items():
> * * *new_list = []
> * * *for inner_data in data_list:
> * * * *if value in inner_data[key]:
> * * * * *new_list.append(inner_data)
> * * *if new_list:
> * * * *result[outer_key] = new_list
> * *return result
>
> Note that this won't work on the 'sec' field, since it's a number rather
> than a string.
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses


I like to perform what I call "objectify" on nested dictionary type
stuff into classes.

class TestPart(object):
def __init__(self, **kwargs):
for k,v in kwargs.items():
setattr(self, k, v)


class Test(object):
def __init__(self):
self.name = None
self.entries = []

def set(self, name, listofdicts):
self.name = name
for l in listofdicts:
self.entries.append(TestPart(**l))

def search(self, key, value):
rets = []
for t in self.entries:
if value in getattr(t, key):
rets.append(t)
return rets

if __name__ == "__main__":
d = {'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}]}
items = []
for k, v in d.items():
t = Test()
t.set(k, v)
items.append(t)

for i in items:
got = i.search("code", "9")
print got
for r in got:
print r.code

It's not quite right, but you get the basic idea. I just find it
easier to wrap my head around structures built like this rather than
trying to remember alot of inner/outer, index variables, etc.

~Sean
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      09-24-2009
On Wed, 23 Sep 2009 14:52:56 -0700 (PDT),
mike171562 <(E-Mail Removed)> 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'
http://www.velocityreviews.com/forums/(E-Mail Removed) | crops where there ain't no crops.
|
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
list + dictionary searching Manoj Python 1 09-01-2008 10:43 AM
Google search result to be URL-limited when searching site, but notwhen searching Web stumblng.tumblr Javascript 1 02-04-2008 09:01 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
Multiset (as dictionary) searching zaharije.pasalic@pmf.unsa.ba C++ 0 07-22-2005 11:23 AM
ot: help searching dictionary for domain names mark | r HTML 1 07-05-2005 09:22 PM



Advertisments