Searching Dictionary

Dave Angel davea at ieee.org
Wed Sep 23 16:17:47 EDT 2009


Support Desk 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}]
> }
>
>   
If you're only going to follow a small part of my suggestion, probably 
better if you had ignored it entirely.  Here is the entire suggestion 
again, for closer study:

"""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.

"""
There are still syntax errors in the fragment you quoted, and you don't 
supply enough context to actually try it.  You don't supply values for 
the class "Key" or for the value, and you don't ask what you are going 
to search for, nor what you expect the result to be.

If I called a mechanic and told him I had trouble starting a motor 
vehicle, but refused to tell him anything else, he might guess a bad 
battery, out of gas, bad starter.  But he probably wouldn't think to ask 
me if I had lost my keys.  And all the stuff about the starter wouldn't 
help if I had neglected to tell him it was a rope-started lawn mower.

And if I did it a second time, he'd probably hang up on me.

My wife's car needed a jump-start today, which is why I'm distracted on 
that theme.

DaveA





More information about the Python-list mailing list