Help with a reverse dictionary lookup
John McMonagle
johnmc at velseis.com.au
Thu Mar 9 19:48:35 EST 2006
On Thu, 2006-03-09 at 15:51 -0800, rh0dium wrote:
> Hi all,
>
> I have a dict which looks like this..
>
> dict={'130nm': {'umc': ['1p6m_1.2-3.3_fsg_ms']},
> '180nm': {'chartered': ['2p6m_1.8-3.3_sal_ms'], 'tsmc':
> ['1p6m_1.8-3.3_sal_log', '1p6m_1.8-3.3_sal_ms']},
> '250nm': {'umc': ['2p6m_1.8-3.3_sal_ms'], 'tsmc':
> ['1p6m_2.2-3.5_sal_log', '1p6m_1.8-3.3_sal_ms']}
> }
>
> For clarification:
> 130nm,180nm,250nm refer to Nodes
> tsmc,chartered,umc refer to Foundries
> 1p6m..blah, 2p6m..blah refer to Process
>
> I want a function which can do this:
>
> nodes = getNodes()
> should return a list [130nm,180nm,250nm]
I'm not going to write your function for you but the following should
help you get started.
dict.keys() <-- returns a list of the keys in your dictionary
[130nm,180nm,250nm]
>
> nodes = getNodes(Foundry="umc")
> should return a list [130nm,250nm]
>
Here you want to check if the key in the child dictionary matches some
text:
Eg:
Foundry = 'umc'
FoundryList = []
for key in dict.keys:
val = dict.get(key)
if Foundry in val.keys():
FoundryList.append(key)
print FoundryList
[130nm,250nm]
> nodes = getNodes(Process="2p6m_1.8-3.3_sal_ms")
> should return a list [180nm,250nm]
>
Same concept here:
Process = ['2p6m_1.8-3.3_sal_ms']
ProcessList = []
for key in dict.keys():
val = dict.get(key)
if Process in val.values():
ProcessList.append(key)
print ProcessList
[180nm,250nm]
So key concepts you need are:
- Get a list of keys from the dictionary <-- dict.keys()
- Get the value matching a key <-- dict.get(key)
- Get a list of values from the dictionary <-- dict.values()
This should be enough to get you going.
> nodes = getNodes(Foundry="umc", Process="2p6m_1.8-3.3_sal_ms")
> should return a list [250nm]
>
> nodes = getNodes(Foundry="foo", Process="2p6m_1.8-3.3_sal_ms")
> should return None
>
> Obviously I want to extend this to Each area ( i.e., getFoundry(),
> getProcess() ) but I can do that. I just don't know how to easily do
> this reverse kind of look up? Can someone help me on this?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
More information about the Python-list
mailing list